Reputation: 441
I have an array of NSNumber
s, for example: 10015, 12313, 10016
I want to check if the array contains the integer I've entered in the searchBar.
My code:
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF CONTAINS[c] %d", [searchText intValue]];
self.searchResults = [newArr filteredArrayUsingPredicate:resultPredicate];
Upvotes: 3
Views: 4128
Reputation: 4231
If you only want to know if the number is in the array, there's no need to use predicates:
BOOL occurs = [newArr containsObject:[NSNumber numberWithInt:[searchText intValue]]];
should do it.
Upvotes: 0
Reputation: 945
If all you need is the boolean value of whether the array contains it then you want.
self.searchResults = [newArr containsObject:@([searchText intValue])];
If you actually want a subset of NSNumber
s in the results (in this case 1), then contains will not work. Have to check equality of the value of each NSNumber
like this...
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF == %d", [searchText intValue]];
Upvotes: 9