Objective c - NSMutableSet unique object property

后端 未结 3 1227
北恋
北恋 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 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).

提交回复
热议问题