我正在尝试将我的代码(用 Swift 3 编写)转换为 Swift 4,为此我在需要的地方添加了 @objc
。 Xcode 在自动修复它们方面做得很好,但我正在努力解决一些问题(都使用相同的 2 种方法),Xcode 无法提供帮助,它只是将 @objc
放在我的代码中的某个地方.
我在我的 ViewController
类中覆盖了一个名为 navbarRightButtonAction(button:)
的方法。
class ViewController: PBViewController {
override func navbarRightButtonAction(button: PBAdaptiveButton) {
...
}
}
这是我收到警告的地方:
Override of instance method 'navbarRightButtonAction(button:)' from extension of PBViewController depends on deprecated inference of '@objc'
然后我认为问题出在 PBViewController
类中,如下所示:
extension PBViewController: PBNavigationBarDelegate {
func navbarRightButtonAction(button: PBAdaptiveButton) {
print("Override this method")
}
}
所以我添加了 @objc func navbarRightButtonAction(button: PBAdaptiveButton)
但它没有帮助。
然后我查看了 PBNavigationBarDelegate
协议(protocol)
protocol PBNavigationBarDelegate {
func navbarRightButtonAction(button:PBAdaptiveButton)
}
我添加了 @objc protocol PBNavigationBarDelegate
但它也没有帮助。
我不知道该怎么做才能修复弃用警告。
最佳答案
将@objc
或@nonobjc
放在extension
前面:
@objc extension PBViewController: PBNavigationBarDelegate
看看Limiting @objc
Inference, SE-0160在 Swift Evolution 了解更多详情。它包含以下有关扩展的示例:
Enabling/disabling @objc inference within an extension
There might be certain regions of code for which all of (or none of) the entry points should be exposed to Objective-C. Allow either @objc or @nonobjc to be specified on an extension. The @objc or @nonobjc will apply to any member of that extension that does not have its own @objc or @nonobjc annotation. For example:
class SwiftClass { }
@objc extension SwiftClass {
func foo() { } // implicitly @objc
func bar() -> (Int, Int) // error: tuple type (Int, Int) not
// expressible in @objc. add @nonobjc or move this method to fix the issue
}
@objcMembers
class MyClass : NSObject {
func wibble() { } // implicitly @objc
}
@nonobjc extension MyClass {
func wobble() { } // not @objc, despite @objcMembers
}
关于ios - 实例方法表单扩展的覆盖取决于 '@objc' 的弃用推理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46819723/