Yosi Dahan
Yosi Dahan

Reputation: 441

NSPredicate with array of NSNumbers

I have an array of NSNumbers, 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

Answers (2)

Chris
Chris

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

Bejmax
Bejmax

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 NSNumbers 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

Related Questions