smtlaissezfaire
smtlaissezfaire

Reputation: 436

NSMutableDictionary hash (hashCode) values are the same for different objects

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)

Upvotes: 0

Views: 1687

Answers (2)

Chris
Chris

Reputation: 40633

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

Upvotes: 1

outis
outis

Reputation: 77400

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.

Upvotes: 4

Related Questions