开发者

Filtering NSMutableArray based on enum property

开发者 https://www.devze.com 2023-01-03 03:16 出处:网络
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

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 filteredArrayUsingPr开发者_运维问答edicate:predicate];

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


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.


- (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];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消