Objective c - NSMutableSet unique object property

后端 未结 3 1222
北恋
北恋 2020-12-17 10:25

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 (u

相关标签:
3条回答
  • 2020-12-17 10:59

    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
    }
    
    0 讨论(0)
  • 2020-12-17 11:06

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

    0 讨论(0)
  • 2020-12-17 11:08

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

    0 讨论(0)
提交回复
热议问题