使用新的Xcode 8.3,我收到错误:
无法覆盖扩展中的非动态类声明
在代码行
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
Run Code Online (Sandbox Code Playgroud)
我怎么能避免这个警告?
我正在尝试实现一个扩展功能,该功能应根据使用它的类的类型而有所不同。这些对象必须是UIView(或子类)。它应该始终使用在指定类型上扩展的函数,但如果不符合任何扩展函数,则应改用UIView方法(作为后备)。
这是我要执行的操作的一个示例:
protocol aProtocol {
typealias completionBlock = (_ finished:Bool)->()
func doSomething(completion: completionBlock)
}
extension UIView: aProtocol {
func doSomething(completion: (Bool) -> ()) {
print("Im an UIView")
}
}
extension aProtocol where Self: UILabel {
func doSomething(completion: (Bool) -> ()) {
print("im an UILabel")
}
}
extension aProtocol where Self: UIImageView {
func doSomething(completion: (Bool) -> ()) {
print("im an UIImageView")
}
}
Run Code Online (Sandbox Code Playgroud)
执行:
UIView().doSomething { (foo) in } // Should print "Im an UIView"
UIButton().doSomething { (foo) in } …Run Code Online (Sandbox Code Playgroud) 如果我有一个类Christmas和一个协议Merry,为了使Christmas符合Merry,许多人会这样做:
class Christmas {
...
}
extension Christmas: Merry {
...
}
Run Code Online (Sandbox Code Playgroud)
它也受到Apple的鼓励.
但是,在定义类时,让类符合协议是不是更方便?像这样:
class Christmas: Merry {
...
}
Run Code Online (Sandbox Code Playgroud)
两种方法有什么区别?