首先,
dispatch_once_t在Swift 3.0中不可用。您可以选择两种选择:
全局变量
静态财产
struct
,enum
或class
令人毛骨悚然的CocoaTouch类
- 令人毛骨悚然的CocoaTouch类,例如UIViewController;
- 定制的Swift类;
例如混写viewWillAppear(_:)
的UIViewController
使用全局变量
自定义Swift类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
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)") }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)