Objective c - NSMutableSet unique object property
Asked Answered
A

3

18

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.

Astigmia answered 14/5, 2012 at 15:18 Comment(0)
B
34

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
}
Bergmann answered 14/5, 2012 at 15:34 Comment(7)
just one thing, if my personId is NSString, how should I override the hash method?Astigmia
Just cast it to an int like this: [self.personId intValue]. And for your comparison use: [self.personId isEqualToString: anObject.personId]Bergmann
@Alladinia: for the hash, that is WRONG. unless the string happens to be a syntactically correct integer, zero will be the result; and the hash value almost always having the same value will result in a rotten performance.Octogenarian
@Octogenarian which part of must be a unique unsigned integer in my code you did not understand? That is why I included the comment (remember, you vote the answers, not the comments). Anyways something like [self.personId hash] would obviously suffice in the case hat personId is not a number.Bergmann
I'm sorry, I wasn't clear in my comment. There is nothing wrong with the answer itself - the issue is with the comment in answer to Eyal's comment, where you suggest getting the hash by casting a string to an int with intValue.Octogenarian
@Octogenarian Understood. But even in the comment there wasn't any clue regarding the form of the string, so assumed to be a string representation of an integer as per the comment on my answer. Having said that, yes I imagine that in the case of a hex uuid for example your suggestion would be true and worth noting.Bergmann
If you are comparing the personId property and it is a string, then obviously the hash function should return self.personId.hash. Strings have their own hash. But alternatively, you can use an NSDictionary* instead of an NSSet; use personId as the key and the object itself as the data. First, it means you don't have to override isEqual and hash (which is an unusual thing to do), and it is much more flexible.Corolla
D
4

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).

Dampier answered 14/5, 2012 at 15:26 Comment(2)
please look I explained more in my questionAstigmia
@Astigmia I think my answer will do what you want; if you add another object with the same personId then the old Person object will be replaced.Dampier
G
0

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 :)

Gunny answered 14/5, 2012 at 15:22 Comment(3)
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 the hard work for me and if I'm adding a person that already exists it won't add it twice..Astigmia
You could use a predicate to check if an object already exists with the same value - see my edits.Gunny
NSMutableSet won't automatically check the person id. It will consider that two objects are the same only if they indeed both point to the same exact objects. You're better off with the dictionary as stated below. It will do the replacing of identical personIDs for you.Floris

© 2022 - 2024 — McMap. All rights reserved.