Method swizzling in swift 4 [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 06:07:49

问题


Swizzling in Swift 4 no longer works.

Method 'initialize()' defines Objective-C class method 'initialize', which is not permitted by Swift

This is something I have found a solution to so wanted to leave the questions and answer for others.


回答1:


initialize() is no longer exposed: Method 'initialize()' defines Objective-C class method 'initialize', which is not permitted by Swift

So the way to do it now is to run your swizzle code via a public static method.

e.g

In the extension: (This extension is used in the kickstarted open source code: https://github.com/kickstarter/ios-oss/blob/master/Library/DataSource/UIView-Extensions.swift)

private var hasSwizzled = false

extension UIView {
    final public class func doBadSwizzleStuff() {
        guard !hasSwizzled else { return }

        hasSwizzled = true
        swizzle(self) /* This is pseudo - run your method here */
    }
}

In the app delegate: (This method is used in the kickstarted open source code: https://github.com/kickstarter/ios-oss/blob/7c827770813e25cc7f79a28fa151cd713efe936f/Kickstarter-iOS/AppDelegate.swift#L33)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{
    UIView.doBadSwizzleStuff()
}

Another way is to use a singleton:

extension UIView {
    static let shared : UIViewController = {
        $0.initialize()
        return $0
    }(UIViewController())

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

        let swizzleClosure: () = {
            UIViewController().swizzle() /* This is pseudo - run your method here */
        }()
        swizzleClosure
    }
}

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: UIApplicationLaunchOptionsKey: Any]?) -> Bool 
{
    _  = UIViewController.shared
}


来源:https://stackoverflow.com/questions/46361065/method-swizzling-in-swift-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!