Arrays hasher.combine(self)在使用集合时会引起问题吗?
使用此Arrays hasher.combine(self)在使用集合时会引起问题吗?,arrays,swift,hashable,Arrays,Swift,Hashable,使用此哈希(into:)实现是否会导致问题,尤其是使用集合和数组时 func hash(into hasher: inout Hasher){ hasher.combine(self) } 我非常确定,hasher.combine(self)要么不会编译,要么会导致无限循环 当hasher.combine()看到给定的类型时,它将查找对象hash(into:)函数,该函数将调用具有相同类型的hasher.combine(),依此类推 你应该做的是 func hash(into hash
哈希(into:)
实现是否会导致问题,尤其是使用集合和数组时
func hash(into hasher: inout Hasher){
hasher.combine(self)
}
我非常确定,
hasher.combine(self)
要么不会编译,要么会导致无限循环
当hasher.combine()
看到给定的类型时,它将查找对象hash(into:)
函数,该函数将调用具有相同类型的hasher.combine()
,依此类推
你应该做的是
func hash(into hasher: inout Hasher) {
hasher.combine(property1)
hasher.combine(prop2)
//...
//...
//...
//...
//...
//...
//until you have written a line to combine every property you want to hash
}
如果您有任何问题,请告诉我。您的Hasher实现导致错误: 线程1:EXC_BAD_访问(代码=2,地址=0x7FFEEC79FE8) 这是使类型符合可哈希协议的默认方式
/// A point in an x-y coordinate system.
struct GridPoint {
var x: Int
var y: Int
}
extension GridPoint: Hashable {
static func == (lhs: GridPoint, rhs: GridPoint) -> Bool {
return lhs.x == rhs.x && lhs.y == rhs.y
}
func hash(into hasher: inout Hasher) {
hasher.combine(x)
hasher.combine(y)
}
}
你为什么要散列
self
?其思想是将属性散列在一起,以创建散列表的散列值。你能提供一些上下文来说明为什么要这样做吗?如果计算机知道散列self
的意思,那么hash(into:)
方法就没有意义了。事实上,该方法的存在是为了定义散列self
的确切含义。在Xcode 11.3版和swift 5上,它确实编译过,但现在您已经解释了它的工作原理,我将不使用它!非常感谢。