Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find objects with the same property value in NSMutableSet

I have NSMutableSet of objects. All objects are unique obviously, but they might have same .angle value, which is NSInteger property.

I need to find out if there are two or more objects with the same .angle value and group then into an array.

How can i do that?
Any guidance would be much appreciated

like image 528
devster Avatar asked May 07 '11 14:05

devster


1 Answers

Use an instance of NSPredicate to filter on the property you're interested in. For example:

NSSet *dogs = [NSSet setWithObjects:
                [Dog dogWithName:@"Fido" age:2],
                [Dog dogWithName:@"Fluffy" age: 3],
                [Dog dogWithName:@"Spot" age:2],
                nil];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"age == %d", 2];
NSSet *twoYearOldDogs = [dogs filteredSetUsingPredicate:predicate];

NSLog(@"%@", twoYearOldDogs);
like image 77
jlehr Avatar answered Nov 13 '22 03:11

jlehr