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 7598857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:30:55+00:00 2026-05-30T22:30:55+00:00

I have a custom class called card and I need to create a set

  • 0

I have a custom class called card and I need to create a set of 10 unique cards from an array of cards of a random size. Also, I need include any whitelisted cards first to make sure they are always included.

My problem is cards from the whitelist (and only the whitelist) are potentially duplicated in the set. The cards randomly added are never duplicated, and the count is always correct (10). I can’t figure out why isEqual seems to work sometimes but not always.

Here is where I create the set (randoms is the array of potential cards to be picked from):

NSMutableSet *randomCards = [NSMutableSet setWithCapacity:10];

[randomCards addObjectsFromArray:whiteListArray];

while ([randomCards count] < 10) {
    NSNumber *randomNumber = [NSNumber numberWithInt:(arc4random() % [randoms count])];
    [randomCards addObject:[randoms objectAtIndex:[randomNumber intValue]]];
}

I overrode the isEqual method for my card class based on another question answered here:

- (BOOL)isEqual:(id)other {

if (other == self)
    return YES;
if (!other || ![other isKindOfClass:[self class]])
    return NO;
return [self isEqualToCard:other];

}

- (BOOL)isEqualToCard:(Card *)myCard {

if (self == myCard) {
    return YES;
}
if ([self cardName] != [myCard cardName] && ![(id)[self cardName] isEqual:[myCard cardName]])
    return NO;

return YES;
}

It seems to work perfectly except when I add in the whitelist cards, I can’t figure out how I’m ending up with duplicates (but never more than 2 copies).

  • 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-30T22:30:57+00:00Added an answer on May 30, 2026 at 10:30 pm

    You need to override hash in addition to isEqual.

    In fact, you always need to make sure these two methods work together. From Apple’s documentation:

    If two objects are equal (as determined by the isEqual: method), they must have the same hash value. This last point is particularly important if you define hash in a subclass and intend to put instances of that subclass into a collection.

    Something like this should work:

    - (NSUInteger)hash {
        return [[self cardName] hash];
    }
    

    This way your hash depends on the same information that you’re using to make the comparison.

    The hash is used by data structures like NSMutableSet to quickly group objects into different buckets. It’s important that if two objects are equal, they have the same hash value. (It’s OK, however, if two objects have the same hash but are not equal. So you could always return the same number from hash, but then your performance would be the same as using an array. Data structures!)

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

Sidebar

Related Questions

I have a custom class called Position. I want to use the following:- Set<Position>
I have a custom class in Obj-C called RouteManager which contains an array of
I need your help. I have write my own custom NSOperation class called GetNewsOperation.
I have a data object -- a custom class called Notification -- that exposes
I have a custom class loader which extends from a URLClassLoader. I added a
I have a custom class called Rows that implements IEnumerable<Row> . I often use
I have a custom class called NoteView that extends FrameLayout . Previously, I had
If I have a custom class called Tires: #import <Foundation/Foundation.h> @interface Tires : NSObject
I have a custom made class called Graph in which I use adjacency lists.
If I have a custom NSObject class called ProgramModel, does it get alloc/init -ed

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.