我想获得符合Hashable的Any
对象的哈希值。
但是,使用此代码:
let anyValue: Any
//...
if let h = anyValue as? Hashable {
return h.hashValue
}
我遇到此错误
Protocol 'Hashable' can only be used as a generic constraint because it has Self or associated type requirements
您需要使用AnyHashable
而不是Hashable
,它是为解决您遇到的特定错误而创建的Hashable
协议的类型擦除版本。
if let h = anyValue as? AnyHashable {