【问题标题】:Override non-dynamic class delaration [duplicate]覆盖非动态类声明[重复]
【发布时间】:2017-08-22 14:21:08
【问题描述】:

使用新的 Xcode 8.3,我收到错误:

不能从扩展覆盖非动态类声明

从代码行:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell

我怎样才能避免这个警告?

【问题讨论】:

标签: ios swift uitableview xcode8 declaration


【解决方案1】:

这意味着,您不能像扩展中的超类委托方法那样覆盖。 你可以做的方式是 mv 扩展覆盖方法来子类声明。

final class DEMO, UITableViewDataSource, UITableViewDelegate { 

    override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
         return sectionHeader
    }

    override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
         return 40
    }

}

【讨论】:

    【解决方案2】:

    扩展可以为类型添加新功能,但它们不能覆盖现有功能。

    你有两个选择:

    1. 将您的方法移动到类范围内,而不是扩展。

    2. dynamic 类型添加到您的方法(定义它的位置),例如示例。

    示例:

    @objc open dynamic func onLog(_ message: String) -> Void {
        print("Info: \(message)");
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-14
      • 1970-01-01
      • 2013-06-03
      • 2016-09-24
      • 1970-01-01
      • 2014-06-17
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      相关资源
      最近更新 更多