因此,我有一个自定义结构,其一个属性为String类型,另一个属性为CLLocationCoordinate2D类型。显然,String符合Hashable,如果我可以扩展CLLocationCoordinate2D使其符合Hashable,那么我的自定义结构也将是Hashable。这是我扩展CLLocationCoordinate2D的尝试:
extension CLLocationCoordinate2D {
static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
}
func hash(into hasher: inout Hasher) {
hasher.combine(self.latitude) //wasn't entirely sure what to put for the combine parameter but I saw similar things online
}
}
您需要Hashable
明确声明:
extension CLLocationCoordinate2D: Hashable {
public static func == (lhs: Self, rhs: Self) -> Bool {
return lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude
}
public func hash(into hasher: inout Hasher) {
hasher.combine(latitude)
hasher.combine(longitude)
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句