如何实现Swizzling swift 3.0的方法? [英] How to implement method swizzling swift 3.0?
问题描述
如何在 Swift 3.0 中实现方法调整?
How can I implement method swizzling in Swift 3.0 ?
我读过 nshipster文章关于它,但是在这个代码的块中
I've read nshipster article about it, but in this code's chunk
struct Static {
static var token: dispatch_once_t = 0
}
编译器给我一个错误
dispatch_once_t在Swift中不可用:使用懒惰初始化的
全局变量
dispatch_once_t is unavailable in Swift: Use lazily initialized globals instead
推荐答案
首先, dispatch_once_t
在Swift 3.0中不可用。
您可以选择以下两种方式:
First of all dispatch_once_t
is unavailable in Swift 3.0.
You can choose from two alternatives:
-
全局变量
Global variable
struct的静态属性
, enum
或 class
有关详细信息,请参阅 Swift 3中的dispatch_once
For more details, see that Whither dispatch_once in Swift 3
- Swizzling CocoaTouch类,例如UIViewController;
- Swizzling自定义Swift类;
Swizzling CocoaTouch类
示例swizzling viewWillAppear(_:)
of UIViewController
使用全局变量
Swizzling CocoaTouch class
example swizzling viewWillAppear(_:)
of UIViewController
using global variable
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自定义Swift类
要使用Swift类的方法调用,必须遵守两个要求(了解更多详情):
- 包含要调配的方法的类必须扩展
NSObject
- 您想要调整的方法必须具有
动态
属性
- The class containing the methods to be swizzled must extend
NSObject
- The methods you want to swizzle must have the
dynamic
attribute
自定义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)")
}
}
这篇关于如何实现Swizzling swift 3.0的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!