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

  • Home
  • SEARCH
  • 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 6977687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:39:28+00:00 2026-05-27T17:39:28+00:00

I have an NSObject subclass in which I implemented the isEqual: and hash methods

  • 0

I have an NSObject subclass in which I implemented the isEqual: and hash methods as follows for testing purposes:

- (BOOL)isEqual:(id)anObject {
    return YES;
}

- (NSUInteger)hash {
    return 1;
}

For some reason, I can add multiple objects of this class to an NSMutableSet even though they are the “same”. Is there any reason this would not be working?


Update: It turns out I was assigning an NSMutableArray instance to an ivar of type NSMutableSet. D’oh!

  • 1 1 Answer
  • 0 Views
  • 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. Editorial Team
    Editorial Team
    2026-05-27T17:39:29+00:00Added an answer on May 27, 2026 at 5:39 pm

    There must be something else in your code that you are not doing right, because overriding methods as you show in the OP causes NSMutableSet to recognize my objects as identical:

    Test.h:

    #import "Foundation/Foundation.h"
    @interface Test : NSObject
    @end
    

    Test.m:

    #import "Test.h"
    @implementation Test
    - (BOOL)isEqual:(id)anObject {
        return YES;
    }
    - (NSUInteger)hash {
        return 1;
    }
    @end
    

    main.c:

    #include <CoreFoundation/CoreFoundation.h>
    #import "Test.h"
    int main (int argc, const char * argv[]) {
        Test *t1 = [[Test alloc] init];
        Test *t2 = [[Test alloc] init];
        Test *t3 = [[Test alloc] init];
        Test *t4 = [[Test alloc] init];
        NSSet *s = [NSMutableSet setWithObjects:t1, t2, t3, t4, nil];
        NSLog(@"%lu", s.count);
        return 0;
    }
    

    This snippet produces 1, as expected.

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

Sidebar

Related Questions

I have created a Class (subclass of NSObject) which will hold all my SQLs/dbConnections
I have a subclass of NSObject, it is a singleton which loads a list
Have just started using Visual Studio Professional's built-in unit testing features, which as I
I have created a class named ListCell which is a subclass of UITableViewCell .
I have a subclass of NSObject that implements an -(id)initWithRootElement:(MyElement *)e method. NSXMLDocument has
I have a superclass and a subclass, both of which define instance variables. Rough
I have subclass of NSObject having 70 properties, i need to observe change in
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I have a NSArrayController with following Object: @interface AdressCard : NSObject <NSCoding> { NSString*
I have a very basic data class that is subclassed from NSObject. I declare

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.