微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 覆盖实例方法表单扩展取决于’@objc’的弃用推断

我试图将我的代码(用 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(按钮:PBAdaptiveButton),但它没有帮助.
然后我查看了PBNavigationBarDelegate协议

protocol PBNavigationBarDelegate {
    func navbarRightButtonAction(button:PBAdaptiveButton)
}

添加了@objc协议PBNavigationBarDelegate,但它也没有帮助.
我不知道如何修复弃用警告.

解决方法

将@objc或@nonobjc放在扩展名前面:

@objc extension PBViewController: PBNavigationBarDelegate

有关详细信息,请查看Swift Evolution的Limiting @objc Inference,SE-0160.它包含以下有关扩展的示例:

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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐