【问题标题】: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】:
扩展可以为类型添加新功能,但它们不能覆盖现有功能。
你有两个选择:
-
将您的方法移动到类范围内,而不是扩展。
-
将dynamic 类型添加到您的方法(定义它的位置),例如示例。
示例:
@objc open dynamic func onLog(_ message: String) -> Void {
print("Info: \(message)");
}