ios – 如何实现方法swizzling swift 3.0?

前端之家收集整理的这篇文章主要介绍了ios – 如何实现方法swizzling swift 3.0?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Swift 3.0中实现方法swizzling?

我已经阅读了nshipster article,但在这段代码

struct Static {
    static var token: dispatch_once_t = 0
}

编译器给我一个错误

dispatch_once_t is unavailable in Swift: Use lazily initialized
globals instead

解决方法

首先,在Swift 3.0中,dispatch_once_t不可用.
您可以从两种选择中选择:

>全局变量
> struct,enum或class的静态属性

有关更多详细信息,请参阅Whither dispatch_once in Swift 3

为了不同的目的,您必须使用不同的swizzling实现

  • Swizzling CocoaTouch class,for example UIViewController;
  • Swizzling custom Swift class;

可旋转的CocoaTouch类

示例使用全局变量调用viewWillAppear(_ :)的UIViewController

private let swizzling: (UIViewController.Type) -> () = { viewController in

    let originalSelector = #selector(viewController.viewWillAppear(_:))
    let swizzledSelector = #selector(viewController.proj_viewWillAppear(animated:))

    let originalMethod = class_getInstanceMethod(viewController,originalSelector)
    let swizzledMethod = class_getInstanceMethod(viewController,swizzledSelector)

    method_exchangeImplementations(originalMethod,swizzledMethod) }

extension UIViewController {

    open override class func initialize() {
        // make sure this isn't a subclass
        guard self === UIViewController.self else { return }
        swizzling(self)
    }

    // MARK: - Method Swizzling

    func proj_viewWillAppear(animated: Bool) {
        self.proj_viewWillAppear(animated: animated)

        let viewControllerName = NSStringFromClass(type(of: self))
        print("viewWillAppear: \(viewControllerName)")
    } 
 }

Swift喜欢Swift的Swift课程

要使用Swift类的方法,您必须遵守以下两项要求(for more details):

>包含要旋转的方法的类必须扩展NSObject
>您想要旋转的方法必须具有动态属性

和例子Swiftzling方法定制Swift基类Person

class Person: NSObject {
    var name = "Person"
    dynamic func foo(_ bar: Bool) {
        print("Person.foo")
    }
}

class Programmer: Person {
    override func foo(_ bar: Bool) {
        super.foo(bar)
        print("Programmer.foo")
    }
}

private let swizzling: (Person.Type) -> () = { person in

    let originalSelector = #selector(person.foo(_:))
    let swizzledSelector = #selector(person.proj_foo(_:))

    let originalMethod = class_getInstanceMethod(person,originalSelector)
    let swizzledMethod = class_getInstanceMethod(person,swizzledMethod)
}

extension Person {

    open override class func initialize() {
        // make sure this isn't a subclass
        guard self === Person.self else { return }
        swizzling(self)
    }

    // MARK: - Method Swizzling

    func proj_foo(_ bar: Bool) {
        self.proj_foo(bar)

        let className = NSStringFromClass(type(of: self))
        print("class: \(className)")
    }
}

猜你在找的iOS相关文章