如何在 Swift 3.0中 实现方法混乱?
我已经阅读了有关nshipster的文章,但是在这段代码中
struct Static { static var token: dispatch_once_t = 0 }
编译器给我一个错误
在Swift中不可用dispatch_once_t:而是使用延迟初始化的全局变量
首先,dispatch_once_t在Swift 3.0中不可用。您可以选择两种选择:
dispatch_once_t
全局变量
静态财产struct,enum或class
struct
enum
class
令人毛骨悚然的CocoaTouch类,例如UIViewController; 定制的Swift类;
例如混写viewWillAppear(_:)的UIViewController使用全局变量
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类使用方法混乱,必须满足两个要求(有关更多详细信息):
NSObject
dynamic
自定义Swift基类的示例混淆方法Person
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, swizzledSelector) method_exchangeImplementations(originalMethod, 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)") } }