快速协议一致性问题

问题描述 投票:0回答:1

我有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'

}

如何解决编译错误?

ios swift protocols
1个回答
0
投票

Swift 不能支持此功能没有根本原因,但到目前为止(到目前为止)还不支持。 This answer很好地解释了。

不幸的是,我认为最好的解决方案是制作两个属性。]

class SomeClass: HelperProtocol {
  weak var strictlyTypedDelegate: (SomeProtocol & AnotherProtocol)?
  var delegate: AnotherProtocol? { strictlyTypedDelegate }
}
    
© www.soinside.com 2019 - 2024. All rights reserved.