我正在尝试实现一个协议(protocol),该协议(protocol)本身继承了多个协议(protocol),这些协议(protocol)都有一个委托(delegate)成员。有没有一种干净的方法可以做到这一点,而不需要为每个协议(protocol)的委托(delegate)使用不同的名称?
protocol ProtocolOne {
var delegate: ProtocolOneDelegate?
}
protocol ProtocolTwo {
var delegate: ProtocolTwoDelegate?
}
protocol CombinedProtocol: ProtocolOne, ProtocolTwo {
}
protocol CombinedDelegate: ProtocolOneDelegate, ProtocolTwoDelegte {
}
class ProtocolImpl: CombinedProtocol {
// How can I implement delegate here?
// I've tried the following options without success:
var delegate: CombinedDelegate?
var delegate: protocol<ProtocolOneDelegate, ProtocolTwoDelegate>?
}
最佳答案
您应该能够将它们合二为一:
var delegate: (ProtocolOneDelegate & ProtocolTwoDelegate)?
您现在可以使用这两种协议(protocol)。
关于Swift 使用委托(delegate)实现多个协议(protocol),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27085158/