Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays hasher.combine(self)在使用集合时会引起问题吗?_Arrays_Swift_Hashable - Fatal编程技术网

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上,它确实编译过,但现在您已经解释了它的工作原理,我将不使用它!非常感谢。