swift里面方法交换怎么实现?

swift里面 load和initialize相继被弃用
方法交换应该怎么实现?
如下方法待验证!

// MARK: let _ = UIViewController.share
extension UIViewController {
    static let share: UIViewController = UIViewController(swizzle: true)
    
    convenience init(swizzle: Bool) {
        self.init()
        
        // viewWillAppear
        let originalSelector = #selector(UIViewController.viewWillAppear(_:))
        let swizzledSelector = #selector(UIViewController.yy_viewWillAppear(animated:))
        
        let originalMethod = class_getInstanceMethod(UIViewController.classForCoder(), originalSelector)
        let swizzledMethod = class_getInstanceMethod(UIViewController.classForCoder(), swizzledSelector)
        
        let didAddMethod = class_addMethod(UIViewController.classForCoder(), originalSelector, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
        
        if didAddMethod {
            class_replaceMethod(UIViewController.classForCoder(), swizzledSelector, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
        } else {
            method_exchangeImplementations(originalMethod!, swizzledMethod!);
        }
        
        // viewDidDisappear
        let originalSelector1 = #selector(UIViewController.viewDidDisappear(_:))
        let swizzledSelector1 = #selector(UIViewController.yy_viewDidDisappear(animated:))
        
        let originalMethod1 = class_getInstanceMethod(UIViewController.classForCoder(), originalSelector1)
        let swizzledMethod1 = class_getInstanceMethod(UIViewController.classForCoder(), swizzledSelector1)
        
        let didAddMethod1 = class_addMethod(UIViewController.classForCoder(), originalSelector1, method_getImplementation(swizzledMethod1!), method_getTypeEncoding(swizzledMethod1!))
        if didAddMethod1 {
            class_replaceMethod(UIViewController.classForCoder(), swizzledSelector1, method_getImplementation(originalMethod1!), method_getTypeEncoding(originalMethod1!))
        } else {
            method_exchangeImplementations(originalMethod1!, swizzledMethod1!)
        }
    }
    
    // MARK: - Method Swizzling
    @objc func yy_viewWillAppear(animated: Bool) {
        self.yy_viewWillAppear(animated: animated)
        print("yy_viewWillAppear" + self.description)
    }
    
    @objc func yy_viewDidDisappear(animated: Bool) {
        self.yy_viewDidDisappear(animated: animated)
        print("yy_viewDidDisappear" + self.description)
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 5,831评论 0 9
  • 之前在写《Category你真的懂吗?》那篇简书收集资料的时候,看了很多load和initialize的资料,加深...
    一剑孤城阅读 7,632评论 3 24
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,403评论 19 139
  • iOS开发中总能看到+load和+initialize的身影,网上对于这两个方法有很多解释,官方也有说明,但有些细...
    朱晓辉阅读 27,570评论 19 139
  • load 和 initialize 两个方法算是两个特殊的类方法了,今天偶然从草稿箱中看到还有本篇未完成的博文,如...
    RITL阅读 5,431评论 8 13