我有2个协议和一个符合它的类。
protocol SomeProtocol: AnyObject { }
protocol AnotherProtocol: AnyObject { }
protocol HelperProtocol {
var delegate: AnotherProtocol? { get }
}
class SomeClass {
weak var delegate: (SomeProtocol & AnotherProtocol)?
}
extension SomeClass: HelperProtocol { // Type 'SomeClass' does not conform to protocol 'HelperProtocol'
}
如何解决编译错误?
Swift 不能支持此功能没有根本原因,但到目前为止(到目前为止)还不支持。 This answer很好地解释了。
不幸的是,我认为最好的解决方案是制作两个属性。]
class SomeClass: HelperProtocol {
weak var strictlyTypedDelegate: (SomeProtocol & AnotherProtocol)?
var delegate: AnotherProtocol? { strictlyTypedDelegate }
}