2010-06-13 125 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]; 
}