Objective c - NSMutableSet unique object property

前提是你 提交于 2019-12-18 04:35:16

问题


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.


回答1:


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
}



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/10586218/objective-c-nsmutableset-unique-object-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!