一尘不染

如何实现swift swift 3.0的方法?

swift

如何在 Swift 3.0中 实现方法混乱?

我已经阅读了有关nshipster的文章,但是在这段代码中

struct Static {
    static var token: dispatch_once_t = 0
}

编译器给我一个错误

在Swift中不可用dispatch_once_t:而是使用延迟初始化的全局变量


阅读 171

收藏
2020-07-07

共1个答案

一尘不染

首先,dispatch_once_t在Swift 3.0中不可用。您可以选择两种选择:

  1. 全局变量

  2. 静态财产structenumclass

对于不同的目的,您必须使用不同的实现方式

  • 令人毛骨悚然的CocoaTouch类,例如UIViewController;
  • 定制的Swift类;

令人毛骨悚然的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类使用方法混乱,必须满足两个要求(有关更多详细信息):

  • 包含要混淆的方法的类必须扩展 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)")
    }
}
2020-07-07