18

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?

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Casting to int and using %d gave me what I needed! Thanks.
7
- (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];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.