Search code examples
objective-cnsmutablearraynspredicatensnumber

Check if NSArray contains some int


I have a NSMutableArray of NSNumbers. Basically I just want to check if any of the NSNumbers in the array = some value.

I could iterate through the array, checking one by one, by this is by no means optimal.

I also tried and failed using containsObject, because this only works if the id's are the same.

I read something about NSPredicate, this seems like a good solution, but I am not sure on how to use it with an NSArray.

Any answer is appreciated.

Thanks


Solution

  • Iterating through the array is the best approach here. This is exactly what the containsObject method is doing under the covers. You could sort the array, but that wouldn't give you very much in terms of efficiency.

    If you want to be able to look up values quicker than O(n), NSArray/NSMutableArray is probably not the right data structure for you.