现在的位置: 首页 > 综合 > 正文

NSObject对象与HashCode

2013年09月13日 ⁄ 综合 ⁄ 共 588字 ⁄ 字号 评论关闭

 

I would assume that NSObject isEquals uses the == operator, and hash uses the memory address.

isEquals method should never uses hash as an absolute test for equality. It is guaranteed to have two objects having similar hashCode, if you search for enough objects (just create more than 2^32 different objects, and at least two of them will have the same hash).

In other words, hash requires the following spec: If two objects are equals, then their hash needs to be equal; however, if two objects' hash values are equals, they are not necessarily equal.

As a tip, you always should override isEquals and hashCode together.

 

一言以蔽之,两个NSObject对象相等,它们的HashCode一定相等。但是反过来不成立。

 


 

抱歉!评论已关闭.