下面的代码会报错
protocol WhatA: AnyObject {
func doThat()
}
protocol WhatB: WhatA {
func doThis()
}
class SomethingA {
weak var delegate: WhatA?
}
class SomethingB: SomethingA {
weak var delegate: WhatB?
}
Property 'delegate' with type 'WhatB?' cannot override a property with type 'WhatA?'
UIKit 对以下没有问题
open class UIScrollView : UIView, NSCoding, UIFocusItemScrollableContainer {
weak open var delegate: UIScrollViewDelegate?
}
open class UITableView : UIScrollView, NSCoding, UIDataSourceTranslating {
weak open var delegate: UITableViewDelegate?
}
为什么这在 UIKit 中有效? this 的可接受答案问题表明这是不可能的。
最佳答案
它与 UIScrollView
和 UITableView
及其委托(delegate)一起工作的原因是它们是从原始 Objective-C header 生成的 Swift 接口(interface)。
Objective-C 可以让你做到这一点。虽然您无法创建直接执行此操作的 Swift 类,但从 Objective-C 桥接 header 生成的 Swift 类接口(interface)可能会导致您在此处看到的情况。
关于ios - 覆盖不同类型的父类(super class)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56096394/