Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering NSMutableArray based on enum property

Tags:

objective-c

I've got an NSMutableArray filled with objects of type "GameObject". GameObject has a number of properties, one of which being "gameObjectType" . "gameObjectType" is of type GameObjectTypeEnum. I want to be able to filter this NSMutableArray so only GameObjects of a certain type are returned. I've got the following in place, but it's giving me a "BAD ACCESS" error:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"gameObjectType = %@", gameObjectType];
return [gameObjects filteredArrayUsingPredicate:predicate];

Is it possible to pass a "custom" type (ie, this enum I've defined) into the predicateWithFormat call?

like image 845
Marty Avatar asked Jun 13 '10 19:06

Marty


2 Answers

The string format specifier %@ indicates an object, while you're passing an integral value. You probably want to typecast the gameObjectType to an int and use the %d specifier. Take a look at the string format specifiers for more info.

like image 124
Chuck Avatar answered Oct 10 '22 19:10

Chuck


- (NSArray *)arrayFilteredByType:(enumType)type {

     //type is an NSUInteger property of the objects in the array 
     NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type = %d", type];
     return [self.array filteredArrayUsingPredicate:predicate];
}
like image 39
Yunus Nedim Mehel Avatar answered Oct 10 '22 19:10

Yunus Nedim Mehel