ios - 实例方法表单扩展的覆盖取决于 '@objc' 的弃用推理

标签 ios swift swift4

我正在尝试将我的代码(用 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/

相关文章:

ios - ViewController 始终加载相同的链接

ios - Swift editActionsForRowAt 与 TrailingSwipeActionsConfigurationForRowAt TableView

swift - Codable 和自定义 Getter 构建错误

ios - 获取 Objective-C/IOS 中特定类的实例计数

ios - 为什么在访问用户的 Facebook 信息时出现此错误?

swift - UITesting Xcode 7 : How to tell if XCUIElement is visible?

ios - 在各部分之间移动行时出现 EXC_BAD_ACCESS

ios - 如何对 ViewController 中的项目进行分组以显示和隐藏?

android - webview nativescript 中的一些外部功能不起作用

ios - 如何使用 AVPlayer 播放 instagram 视频