Tikhonov Alexander Tikhonov Alexander - 1 year ago 445
iOS Question

How to implement method swizzling swift 3.0?

How can I implement method swizzling in Swift 3.0 ?

I've read nshipster article about it, but in this code's chunk

struct Static {
static var token: dispatch_once_t = 0

the compiler gives me an error

dispatch_once_t is unavailable in Swift: Use lazily initialized
globals instead

Answer Source

First of all dispatch_once_t is unavailable in Swift 3.0. You can choose from two alternatives:

  1. Global variable

  2. Static property of struct, enum or class

For more details, see that Whither dispatch_once in Swift 3

I chose global variable, I think it's way more clear.

fileprivate 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)

        let didAddMethod = class_addMethod(viewController, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod))

        if didAddMethod {
            class_replaceMethod(viewController, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod))
        } else {
            method_exchangeImplementations(originalMethod, swizzledMethod)

    extension UIViewController {

        open override class func initialize() {
            // make sure this isn't a subclass
            guard self === UIViewController.self else { return }

        // MARK: - Method Swizzling

        func proj_viewWillAppear(animated: Bool) {
            self.proj_viewWillAppear(animated: animated)

             let viewControllerName = NSStringFromClass(type(of: self))
             print("viewWillAppear: \(viewControllerName)")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download