简体   繁体   English

swift4中可哈希协议的用途是什么?

[英]What is the use of hashable protocol in swift4?

please explain the use of hashable protocol with implementation in swift. 请快速解释如何使用可哈希协议。 Apple defines hashable as “a type that provides an integer a hash value.” Okay, but what's a hash value? Apple将hashable定义为“一种为整数提供哈希值的类型。”好的,但是什么是哈希值?

To make an object conform to Hashable we need to provide a hashValue property that will return a unique, consistent number for each instance. 为了使对象符合Hashable,我们需要提供一个hashValue属性,该属性将为每个实例返回唯一的一致编号。 The Hashable protocol inherits from Equatable, so you may also need to implement an == function. Hashable协议继承自Equatable,因此您可能还需要实现==函数。

Note: If two objects compare as equal using == they should also generate the same hash value, but the reverse isn't true – hash collisions can happen. 注意:如果两个对象使用==比较相等,则它们也应生成相同的哈希值,但事实并非如此-可能会发生哈希冲突。

Before Swift 4.1, conforming to Hashable was complex because you needed to calculate a hashValue property by hand. 在Swift 4.1之前,遵循Hashable的操作很复杂,因为您需要手动计算hashValue属性。 In Swift 4.1 this improved so that hashValue could be synthesized on your behalf if all the properties conform to Hashable . 在Swift 4.1中,此功能得到了改进,因此,如果所有属性都符合Hashable,则可以代表您合成hashValue。 Swift 4.2 introduces a new Hasher struct that provides a randomly seeded, universal hash function to make all our lives easier. Swift 4.2引入了新的Hasher结构,该结构提供了随机种子的通用哈希函数,使我们的生活更轻松。 Refer for more 参考更多

If an object conforms to the hashable protocol, it needs to have a hashValue , as you mentioned. hashValue ,如果对象符合可hashable协议,则它必须具有hashValue The hashValue can be used to compare objects / uniquely identify the object. hashValue可用于比较对象/唯一标识对象。

You can compare objects in two ways: 您可以通过两种方式比较对象:

  1. === function. ===功能。 This checks object references (can only be used with classes). 这将检查对象引用(只能与类一起使用)。 It checks if the left object has the same reference to the right object. 它检查左侧对象是否与右侧对象具有相同的引用。 Even if both objects have exactly the same property values BUT they do have a different reference, it returns false. 即使两个对象都具有完全相同的属性值,但它们确实具有不同的引用,它也会返回false。

  2. == function ( Equatable protocol). ==功能( Equatable协议)。 It checks if the objects are equal to eachother based on the static func == . 它根据静态func ==检查对象是否彼此相等。 You can return the hashValue of the object. 您可以返回对象的hashValue In that way, you can say objects are equal to eachtoher based on the properties, rather than reference. 这样,您可以说对象是基于属性而不是引用相等的。

If you provide your own hashValue , you can say objects are equal to eachother in a way you say objects are equal to eachother, regardless of the reference to the object. 如果提供自己的hashValue ,则可以说对象彼此相等,而无论引用对象是什么。 You can use objects in a Set that conform to the hashable protocol, because a Set checks if objects are equal to eachother based on the hashValue . 您可以在Set中使用符合可哈希协议的对象,因为Set会基于hashValue检查对象是否彼此相等。

Quick answer: 快速回答:

We use hash integer into object to be able to quickly identify object that are equals by getting the object instance in front of the index that we are looking for. 我们使用哈希整数到对象中,从而能够通过将对象实例置于要查找的索引的前面来快速标识相等的对象。

Not quick answer: 没有快速回答:

When you are dealing with list in order to find an object you need to iterate over all your array and compare property to find the one you are looking for, this can slowdown your app as the list get bigger. 当您处理列表以查找对象时,您需要遍历所有数组并比较属性以查找所需的对象,这会随着列表变大而使您的应用变慢。

When you use SET, the mechanism under the hood use hash indexes to find an object so it take you only the time to calculate once the index you are looking for then you can access straight forward to your object , THIS IS SO COOL ISN'T. 使用SET时,内部机制使用哈希索引来查找对象,因此仅需花费时间即可计算出要查找的索引,然后就可以直接访问您的对象 ,这太酷了。 In order to use SET the object need to conform to Hashable protocol since Swift 4.1, if your class or struct and all properties conform to Hashable then the conformity with Hashable and Equatable protocol is automatically done for you under the hood. 为了使用SET,自Swift 4.1起,该对象需要符合Hashable协议,如果您的类或结构以及所有属性均符合Hashable,则将在后台自动为您实现与Hashable和Equatable协议的符合性。 If you do not meet those requirments then you will have to make sure that you conform to Equatable and Hashable protocol. 如果您不满足这些要求,则必须确保您符合Equatable和Hashable协议。

Equatable protocol need to overide static func ==(..) in order to compare you object. 等同协议需要覆盖static func ==(..) ,以便比较您的对象。

Hashable protocol need to provide as far as you can a unique integer value hashValue which need to be the same within two object when they are equals. 哈希协议需要尽可能地提供唯一的整数值hashValue ,当两个对象相等时,它们必须在两个对象中相同。 Hope this help 希望这个帮助

The Hashable documentation give one concrete example of what it's for: Hashable文档给出了一个具体的示例:

You can use any type that conforms to the Hashable protocol in a set or as a dictionary key. 您可以在集合中或字典字典中使用任何符合Hashable协议的类型。

You can think of a hash value as a quick approximation of equality. 您可以将哈希值视为相等的快速近似。 Two elements that are equal will have the same hash value but two elements with the same hash value are not guaranteed to actually be equal. 两个相等的元素将具有相同的哈希值,但不能保证两个具有相同哈希值的元素实际上是相等的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM