Objective c - NSMutableSet unique object property

7.9k views Asked by At

In my app I have a class Person with personId property.
Now I need some data structure to hold a bunch of unique Person objects (unique = different personId)

So I guess I should use NSMutableSet as my data structure, but how do I make the NSMutableSet compare the personId property when adding a person (so I won't add the same person more then ones)?

My goal is to have a collection of unique persons all the time (even if I add two persons with the same id), I want that the NSMutableSet will do all the hard work for me and if I'm adding a person that already exists it won't add it twice.

3

There are 3 answers

7
Alladinian On BEST ANSWER

You can achieve that by understanding how the comparison is made by the NSSet. When you add a new object to the set, isEqual: method is called (it's an NSObject method) against each of the set's elements. So what you can do is override this method and provide a custom comparison like this:

NOTE:

If you override isEqual: method you must override hash method as well

// In your Person.m
- (BOOL)isEqual:(id)anObject
{
    return [self.personId isEqual:anObject.personId]; 
    // If it's an object. Otherwise use a simple comparison like self.personId == anObject.personId
}

- (NSUInteger)hash
{
    return self.personId; //Must be a unique unsigned integer
}
3
Zack Brown On

You shouldn't need to worry about this when you're adding items to an array. As long as you're adding a unique object each time, the properties of the object shouldn't matter.

If you're worried about this and want to be doubly sure, use NSMutableSet instead (unless you need to maintain order of each object).

Update

You could use a predicate to check that an object with the same value does not exist before adding it. This would work fine with both NSMutableArray and NSMutableSet.

//create array
NSMutableArray *array = [[NSMutableArray alloc] init];

//create person
Person *newPerson = [[Person alloc] init];

//add person
[array addObject:newPerson]

//check new object exists
if([[array filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"personID == %@", [newPerson personID]]] count] == 0)
{
    //add object
    [array addObject:newPerson];
}

Very rough pseudocode but hopefully you get the gist :)

2
trojanfoe On

I think what you want is an NSMutableDictionary, mapping the personId to the Person object.

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

Person *person = something;
[dict setObject:personObj forKey:[NSNumber numberWithInt:[person personId]]];
... etc ...

(don't forget to release dict later somewhere).

In order to find the Person class for the specified personId (12, say), you can simply do:

Person *person = [dict objectForKey:[NSNumber numberWithInt:12]];

Note: that you have to store both the key and the value as objects (you cannot store primitive types).