Swift 4 + swizzle viewWillAppear()

时间:2017-11-28 18:08:48

标签: ios swift uiviewcontroller swift4 swizzling

我的问题不在于如何调整,而是在这个特定的代码段中发生了什么:

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 CocoaTouch class

我的问题是围绕以下代码行:

// make sure this isn't a subclass
guard self === UIViewController.self else { return }

为什么我们需要检查它是否不是UIViewController的子类?我的方案是我想将带有视图名称的分析数据发送到Omniture(在viewWillAppear上)。如果我进行检查,那么调配工作永远不会有效,但当我对这一行进行评论时,我得到了我想要的结果,并且每个视图控制器都会发送数据。

1 个答案:

答案 0 :(得分:2)

您查看的示例似乎来自于在initialize类方法中完成此操作的日期,您需要进行检查,或者您最终可能会多次运行相同的代码(当第一次使用子类时),这会导致您调整已经混合的方法。

但是Swift中不再允许使用initialize方法,因此该行不适用。