gpt4 book ai didi

ios - 在 Swift 3.1 中调配 CocoaTouch 类

转载 作者:可可西里 更新时间:2023-11-01 05:43:03 27 4
gpt4 key购买 nike

我在 XCode 8.3 中使用 Swift 3.1 并看到警告:

Method 'initialize()' defines Objective-C class method 'initialize', which is not guaranteed to be invoked by Swift and will be disallowed in future versions

我使用 Swizzling CocoaTouch class并且对该部分有疑问:

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)")
}
}

如何重写那部分代码?

open override class func initialize()

修复新警告?

我看到了link ,但我不明白如何在我的代码中使用信息。

最佳答案

我遇到了同样的问题并解决了它。

我的解决方案

1。将 swizzling() 方法从私有(private)更改为公共(public)

public let swizzling: (AnyClass, Selector, Selector) -> () = { forClass, originalSelector, swizzledSelector in
let originalMethod = class_getInstanceMethod(forClass, originalSelector)
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector)
method_exchangeImplementations(originalMethod, swizzledMethod)
}

2。移除 extension UIViewController

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

3。在 AppDelegate::didFinishLaunchingWithOptions 添加 swizzling()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

// some code

swizzling(UIViewController.self)

return true
}

这是简单/简单的解决方案。 (这不是优雅)

如果你想获得更多信息,请看这里: Swift 3.1 deprecates initialize(). How can I achieve the same thing?

关于ios - 在 Swift 3.1 中调配 CocoaTouch 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43119514/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com