¿Cómo implementar el swizzling swift de método 3.0?

¿Cómo puedo implementar el método swizzling en Swift 3.0 ?

Leí un artículo de nshipster al respecto, pero en el trozo de este código

struct Static { static var token: dispatch_once_t = 0 } 

el comstackdor me da un error

dispatch_once_t no está disponible en Swift: utiliza globalmente inicializados perezosamente

Primero, dispatch_once_t no está disponible en Swift 3.0. Puede elegir entre dos alternativas:

  1. Variable global

  2. Propiedad estática de struct , enum or class

Para más detalles, vea que Whither dispatch_once en Swift 3

Para diferentes propósitos debe usar diferentes implementaciones de swizzling

  • Clase Swizzling CocoaTouch, por ejemplo, UIViewController;
  • Swizzling class Swift personalizada;

Swizzling CocoaTouch class

ejemplo swizzling viewWillAppear(_:) de UIViewController usando variable global

 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)") } } 

Swizzling custom Swift class

Para usar el método swizzling con sus classs Swift hay dos requisitos que debe cumplir ( para más detalles ):

  • La class que contiene los methods a swizzled debe extender NSObject
  • Los methods que desea swizzle deben tener el atributo dynamic

Y ejemplo de swizzling método de la class base Swift personalizada 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)") } } 

@TikhonovAlexander: gran respuesta

Modifiqué el swizzler para tomar ambos selectores y hacerlo más genérico.

Swift 3

 private let swizzling: (AnyClass, Selector, Selector) -> () = { forClass, originalSelector, swizzledSelector in let originalMethod = class_getInstanceMethod(forClass, originalSelector) let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) method_exchangeImplementations(originalMethod, swizzledMethod) } extension UIView { open override class func initialize() { // make sure this isn't a subclass guard self === UIView.self else { return } let originalSelector = #selector(layoutSubviews) let swizzledSelector = #selector(swizzled_layoutSubviews) swizzling(self, originalSelector, swizzledSelector) } func swizzled_layoutSubviews() { swizzled_layoutSubviews() print("swizzled_layoutSubviews") } } 

Swift 4

 private let swizzling: (AnyClass, Selector, Selector) -> () = { forClass, originalSelector, swizzledSelector in let originalMethod = class_getInstanceMethod(forClass, originalSelector) let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) method_exchangeImplementations(originalMethod, swizzledMethod) } extension UIView { static let classInit: Void = { let originalSelector = #selector(layoutSubviews) let swizzledSelector = #selector(swizzled_layoutSubviews) swizzling(UIView.self, originalSelector, swizzledSelector) }() func swizzled_layoutSubviews() { swizzled_layoutSubviews() print("swizzled_layoutSubviews") } } @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { override init() { super.init() UIView.classInit } } 

swizzling en el patio de recreo

 class TestSwizzling : NSObject { dynamic func methodOne()->Int{ return 1 } } extension TestSwizzling { //In Objective-C you'd perform the swizzling in load(), //but this method is not permitted in Swift func initialize1() { let i: () -> () = { let originalSelector = #selector(TestSwizzling.methodOne) let swizzledSelector = #selector(TestSwizzling.methodTwo) let originalMethod = class_getInstanceMethod(TestSwizzling.self, originalSelector); let swizzledMethod = class_getInstanceMethod(TestSwizzling.self, swizzledSelector) method_exchangeImplementations(originalMethod, swizzledMethod) print("swizzled") } i() } func methodTwo()->Int{ // It will not be a recursive call anymore after the swizzling return 4 } } var c = TestSwizzling() c.initialize1() print(c.methodOne()) print(c.methodTwo()) 

salida: 4 1