ios - 覆盖不同类型的父类(super class)属性

标签 ios swift syntax

下面的代码会报错

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 的可接受答案问题表明这是不可能的。

最佳答案

它与 UIScrollViewUITableView 及其委托(delegate)一起工作的原因是它们是从原始 Objective-C header 生成的 Swift 接口(interface)。

Objective-C 可以让你做到这一点。虽然您无法创建直接执行此操作的 Swift 类,但从 Objective-C 桥接 header 生成的 Swift 类接口(interface)可能会导致您在此处看到的情况。

关于ios - 覆盖不同类型的父类(super class)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56096394/

相关文章:

ios - 还有其他人无法将XCode更新到4.3.1吗?

ios - MFMailComposeViewController 不关闭

swift - 剥离货币字符字符串的优雅方法?

sql - 加入mysql5

rust - 是否可以将枚举变体的所有结构成员绑定(bind)到单个变量?

android - 移动设备上的图像处理速度

ios - 自定义 UIButton 阴影

swift - 设置为共享实例

swift - Swift 中类似控制中心的滑动 View 实现

javascript - 变量 x, y = 'foo' ;这能实现吗?