Swift 哈希表(Hashable)
在本教程中,我们将通过示例了解 Swift 哈希表(Hashable)。
在 Swift 中,Hashable 是为对象提供 hashValue (哈希值)的协议。hashValue 用于比较两个实例。
要使用 hashValue,我们首先必须将类型(struct、class 等)与 Hashable 属性相一致(关联)。例如,
struct Employee: Hashable {...}
在这里,我们将 Employee 结构与 Hashable 协议相一致。
现在,当我们创建 Employee 的实例时,协议将为实例提供哈希值。
实例: Swift 哈希表协议
struct Employee: Hashable {var name: String}let object1 = Employee(name: "Sabby")let object2 = Employee(name: "Smith")// print hash valuesprint(object1.hashValue)print(object2.hashValue)
结果为:
754256748862435114-6408043519205941253
在这里,object1.hashValue 和 object2.hashValue 分别返回实例 object1 和 object2 的哈希值。
注意:哈希值是一个长整数,它根据您使用的系统而变化,因此对于同一代码,您可能会得到不同的值。
使用哈希协议比较实例
// conform Employee to Hashablestruct Employee: Hashable {var name: Stringvar salary: Int}// initialize two objects with different property valueslet obj1 = Employee(name: "Sabby", salary: 40000)let obj2 = Employee(name: "Cathy", salary: 30000)print("Different hash value: ")print(obj1.hashValue)print(obj2.hashValue)// initialize two objects with same property valueslet obj3 = Employee(name: "Lanny", salary: 50000)let obj4 = Employee(name: "Lanny", salary: 50000)print("\nSame hash value: ")print(obj3.hashValue)print(obj4.hashValue)
结果如下:
Different hash value:39349536787678339064997634560615333199Same hash value:15881294381685293181588129438168529318
在上面的实例中,我们创建了一个名为 Employee 的结构,它符合 Hashable 协议。
我们已经创建了两个对象 obj1 和 obj2。
这一次,obj3 和 obj4 的属性值是相同的,因此我们为实例获得了相同的哈希值。
哈希函数和组合
在上面的示例中,我们比较了结构的所有属性。
然而,有时我们可能需要比较类型的选择特性。在这种情况下,我们可以在类型内使用哈希函数。例如,
func hash(into hasher: inout Hasher) {hasher.combine(name)}
这里,hash() 函数使用 hasher.combine() 指定要比较的属性。
使用哈希函数
struct Employee: Hashable {var name: Stringvar salary: Int// create a hash() function to only compare age propertyfunc hash(into hasher: inout Hasher) {hasher.combine(salary)}}// initialize two objects with different values for salary propertylet obj1 = Employee(name: "Sabby", salary: 349879)let obj2 = Employee(name: "Sabby", salary: 422532)print(obj1.hashValue)print(obj2.hashValue)
结果如下:
3932232896576771782743881919875172951
在上面的实例中,我们使用 hash() 函数根据 salary 属性比较了两个实例。
func hash(into hasher: inout Hasher) {hasher.combine(salary)}
这里,ob1 和 obj2 的 salary 薪水不同,所以我们得到不同的哈希值。
如果我们在 hash() 函数中使用了 name 属性,我们将得到相同的哈希值。这是因为两个对象的名称相同。
func hash(into hasher: inout Hasher) {hasher.combine(age)}// Output:// 976332112043722041// 976332112043722041