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

ios – 如何实现方法swizzling swift 3.0?,第1张

概述如何在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 global 如何在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 – 如何实现方法swizzling swift 3.0?全部内容,希望文章能够帮你解决ios – 如何实现方法swizzling swift 3.0?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1115159.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存