NSMutableDictionary 不同对象的哈希值(hashCode)是相同的
Here's an example of two different dictionaries, yet they return the same hash code. Why?
https://gist.github.com/837861
(They aren't the same object)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不保证不同对象的哈希值是不同的。事实上,哈希冲突是会发生的。唯一的两个属性
-hash
方法应该保证(均取自文档):如果两个对象相等(由isEqual: 方法),它们必须具有相同的哈希值。
如果将可变对象添加到使用哈希值确定对象在集合中的位置的集合中,则当对象位于集合中时,对象的哈希方法返回的值不得更改。
Hashes aren't guaranteed to be distinct for distinct objects. In fact, hash collisions will happen. The only two properties the
-hash
method is supposed to guarantee are (both taken from the documentation):If two objects are equal (as determined by the isEqual: method), they must have the same hash value.
If a mutable object is added to a collection that uses hash values to determine the object’s position in the collection, the value returned by the hash method of the object must not change while the object is in the collection.
如果您查看此处,您可以看到字典上的哈希实现只是返回计数,这可能是您获得相同代码的原因:
https://stackoverflow.com/a/11984624/59198
If you look here, you can see that the hash implementation on dictionaries simply returns the count and is likely the reason why you're getting the same code:
https://stackoverflow.com/a/11984624/59198