Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 68005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:20:56+00:00 2026-05-10T19:20:56+00:00

How do you properly override isEqual: in Objective-C? The catch seems to be that

  • 0

How do you properly override isEqual: in Objective-C? The ‘catch’ seems to be that if two objects are equal (as determined by the isEqual: method), they must have the same hash value.

The Introspection section of the Cocoa Fundamentals Guide does have an example on how to override isEqual:, copied as follows, for a class named MyWidget:

- (BOOL)isEqual:(id)other {     if (other == self)         return YES;     if (!other || ![other isKindOfClass:[self class]])         return NO;     return [self isEqualToWidget:other]; }  - (BOOL)isEqualToWidget:(MyWidget *)aWidget {     if (self == aWidget)         return YES;     if (![(id)[self name] isEqual:[aWidget name]])         return NO;     if (![[self data] isEqualToData:[aWidget data]])         return NO;     return YES; } 

It checks pointer equality, then class equality, and finally compares the objects using isEqualToWidget:, which only checks the name and data properties. What the example doesn’t show is how to override hash.

Let’s assume there are other properties that do not affect equality, say age. Shouldn’t the hash method be overridden such that only name and data affect the hash? And if so, how would you do that? Just add the hashes of name and data? For example:

- (NSUInteger)hash {     NSUInteger hash = 0;     hash += [[self name] hash];     hash += [[self data] hash];     return hash; } 

Is that sufficient? Is there a better technique? What if you have primitives, like int? Convert them to NSNumber to get their hash? Or structs like NSRect?

(Brain fart: Originally wrote ‘bitwise OR’ them together with |=. Meant add.)

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:20:56+00:00Added an answer on May 10, 2026 at 7:20 pm

    Start with

     NSUInteger prime = 31;  NSUInteger result = 1; 

    Then for every primitive you do

     result = prime * result + var 

    For objects you use 0 for nil and otherwise their hashcode.

     result = prime * result + [var hash]; 

    For booleans you use two different values

     result = prime * result + ((var)?1231:1237); 

    Explanation and Attribution

    This is not tcurdt’s work, and comments were asking for more explanation, so I believe an edit for attribution is fair.

    This algorithm was popularized in the book ‘Effective Java’, and the relevant chapter can currently be found online here. That book popularized the algorithm, which is now a default in a number of Java applications (including Eclipse). It derived, however, from an even older implementation which is variously attributed to Dan Bernstein or Chris Torek. That older algorithm originally floated around on Usenet, and certain attribution is difficult. For example, there is some interesting commentary in this Apache code (search for their names) that references the original source.

    Bottom line is, this is a very old, simple hashing algorithm. It is not the most performant, and it is not even proven mathematically to be a ‘good’ algorithm. But it is simple, and a lot of people have used it for a long time with good results, so it has a lot of historical support.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a setter method (setMinimumNumberOfSides) that I want to override after using synthesize.
How can I override the IsValid property? I have a model that's validating as
Possible Duplicate: How to properly clean up Excel interop objects in C# I have
If I have two arrays a and b , what method should the object
I am working on CustomSiteMapProvider that i have Derived from StaticSiteMapProvider. I have override
I have a ExpandableListView in my activity that works properly. I set in my
Problem I currently have a factory that depends on a few parameters to properly
i have created a module and it's override magento hash function properly. But question
I have override for controller that checks if certain session data exists. This data
I have OnPaint method overrided to draw an Ellipse on the screen. protected override

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.