为什么以下代码会产生错误?
protocol ProtocolA {
var someProperty: ProtocolB { get }
}
protocol ProtocolB {}
class ConformsToB: ProtocolB {}
class SomeClass: ProtocolA { // Type 'SomeClass' does not conform to protocol 'ProtocolA'
var someProperty: ConformsToB
init(someProperty: ConformsToB) {
self.someProperty = someProperty
}
}
这个类似问题的答案很有道理。 但是在我的例子中,该属性是只读的。为什么这样做不起作用?这是 Swift 的缺陷还是有其他原因呢?
ProtocolA
中应该有associatedtype T: ProtocolB
,然后声明var someProperty: T { get }
。 - BallpointBen