我正在尝试为基于左右操作数标识的协议实现 Equatable 协议。换句话说:我如何为一个协议实现 Equatable 协议,以确定实现该协议的两个实例(在我的情况下iNetworkSubscriber
)是否相同(相同的对象引用)。像那样(错误消息包含在下面的代码中):
protocol iNetworkSubscriber : Equatable {
func onMessage(_ packet: NetworkPacket)
}
func ==(lhs: iNetworkSubscriber, rhs: iNetworkSubscriber) -> Bool { // <- Protocol 'iNetworkSubscriber' can only be used as a generic constraint because it has Self or associated type requirements
return ObjectIdentifier(lhs) == ObjectIdentifier(rhs) // <- Cannot invoke initializer for type 'ObjectIdentifier' with an argument list of type '(iNetworkSubscriber)'
}
...我还尝试了身份运算符本身:
func ==(lhs: iNetworkSubscriber, rhs: iNetworkSubscriber) -> Bool { // <- Protocol 'iNetworkSubscriber' can only be used as a generic constraint because it has Self or associated type requirements
return lhs === rhs // <- Binary operator '===' cannot be applied to two 'iNetworkSubscriber' operands
}
有人知道如何解决这个问题吗?