2010-06-13 10 views
17

Tengo un NSMutableArray lleno de objetos del tipo "GameObject". GameObject tiene varias propiedades, una de las cuales es "gameObjectType". "gameObjectType" es de tipo GameObjectTypeEnum. Quiero poder filtrar este NSMutableArray para que solo se devuelvan GameObjects de un cierto tipo. Tengo el siguiente en su lugar, pero me está dando una "ACCESO BAD" error:Filtrado NSMutableArray basado en la propiedad enum

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

¿Es posible pasar un tipo "custom" (es decir, esta enumeración he definido) en el predicateWithFormat ¿llamada?

Respuesta

21

El especificador de formato de cadena %@ indica un objeto, mientras está pasando un valor integral. Probablemente quiera encasillar el gameObjectType en un int y usar el especificador %d. Eche un vistazo a string format specifiers para más información.

+0

¡Fundir en int y usar% d me dio lo que necesitaba! Gracias. – Marty

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]; 
} 
Cuestiones relacionadas