我想知道下面的代码出了什么问题?
import Foundation
enum SliderType: Int {
case analog = 1, discrete, highLow
}
protocol DataEntry: class, Hashable {
var hashValue: Int { get set } // hashable protocol requires this
var idx: Int { get set }
var category: String { get set }
var sliderType: SliderType { get set }
var sliderTitle: String { get set }
var sliderCurrentValue: Float { get set }
var sliderMinValue: Float { get set }
var sliderMaxValue: Float { get set }
}
func ==(lhs: DataEntry, rhs: DataEntry) -> Bool {
return lhs.idx == rhs.idx
}在这个屏幕截图中可以看到,我一直收到错误Protocol 'DataEntry' can only be used as a generic constraint because it has Self or associated type requirements
有人知道这里可能出了什么问题吗??如何在协议中实现Hashable协议?

发布于 2017-02-20 03:18:33
==要求lhs和rhs的类型相同。拥有两个类型的DataEntry是不够的,因为您可以将lhs设置为FooDataEntry,将rhs设置为BarDataEntry。
为了加强lhs和rhs之间的这种关系,您需要使用泛型。
func ==<T: DataEntry>(lhs: T, rhs: T) -> Bool {
return lhs.idx == rhs.idx
}https://stackoverflow.com/questions/42331607
复制相似问题