2010-06-13 127 views
17

我有一个NSMutableArray填充类型为“GameObject”的对象。 GameObject有许多属性,其中之一是“gameObjectType”。 “gameObjectType”的类型是GameObjectTypeEnum。我想能够过滤这个NSMutableArray,所以只返回某种类型的GameObjects。我有到位以下,但它给了我一个“BAD访问”错误:基于枚举属性过滤NSMutableArray

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

是否有可能通过“自定义”类型(即,此枚举我定义)到predicateWithFormat呼叫?

回答

21

字符串格式说明%@指示对象,当你传递一个整数值。您可能想要将gameObjectType转换为int并使用%d说明符。看看string format specifiers了解更多信息。

+0

铸造到int和使用%d给了我我需要的!谢谢。 – Marty 2010-06-13 19:29:19

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