• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: hash and isEqual not working as expected
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hash and isEqual not working as expected


  • Subject: Re: hash and isEqual not working as expected
  • From: Dustin Voss <email@hidden>
  • Date: Sun, 11 Jan 2004 14:05:26 -0800

On 10 Jan, 2004, at 8:06 AM, Eyal Redler wrote:

Hi All,

Until now I thought that if I don't define hash for my classes things will not work well. I found a few classes where I forgot to define hash and discovered that there is no difference:
Even though the class breaks the rule saying "If two objects are equal (as determined by the isEqual: method), they must return the same hash value" the behavior inside Arrays dictionaries and attributed string seem to be OK.

In the following example you'll notice that hash is not defined for MyClass but the dictionary equality test doesn't fail and the string attribute is correctly spread out across the whole string. The hash results are different but they don't seem to be called from the comparison code.

Is there an explanation for this?

You bet.

The only classes that use hashes are NSDictionary and NSSet (and their sub-classes). Dictionaries only use the hash values to look up an object via key. Sets use hash values to check membership. The proscription in the documentation about hash and isEqual: is simply to ensure that classes you write will work when used with collections.

If you had put objectA in an NSMutableSet and tried to add objectB, it should not succeed, because objectA = objectB and objectA was already in the set, but it would succeed because you did not implement the hash method. Similarly, (though the docs aren't clear) you should not be able to add objects to a dictionary using both objectA and objectB, but you would be able to.

Now, the dictionary equality check succeeds because the two dictionaries have the same number of entries and the same keys, and because objectA in the first dictionary and objectB in the second dictionary are also the same according to isEqual:, which is what the dictionary equality test uses. So this outcome is completely expected.

The page http://cocoadev.com/index.pl?HashingAndEquality might help you understand this stuff.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
References: 
 >hash and isEqual not working as expected (From: Eyal Redler <email@hidden>)

  • Prev by Date: Re: Tutorials using NSDrawer???
  • Next by Date: NSForm keyReponder ?
  • Previous by thread: hash and isEqual not working as expected
  • Next by thread: Any word on GarageBand plug-ins
  • Index(es):
    • Date
    • Thread