【发布时间】:2017-01-14 23:54:32
【问题描述】:
我希望为我声明的协议提供Comparable 的默认实现。以下给出错误Extension of protocol 'Asset' can not have an inheritance clause
protocol Asset {
func getPriority() -> AssetPriority
}
extension Asset: Comparable {
static func < (lhs: Asset, rhs: Asset) -> Bool {
return lhs.getPriority() < rhs.getPriority()
}
static func == (lhs: Asset, rhs: Asset) -> Bool {
return lhs.getPriority() == rhs.getPriority()
}
}
我知道我可以进行协议继承(没有扩展):
protocol Asset: Comparable {
func getPriority() -> AssetPriority
}
但是我必须一遍又一遍地实现相同的两个功能。
我不希望每个资产都继承自一个公共基类(因为这违背了协议的目的......)
在 Swift 3 中尚不支持协议的协议扩展(就像它用于类一样)?
【问题讨论】: