Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 411
Henry Avatar asked Aug 07 '11 06:08

Henry


1 Answers

Inspired by bernstein I looked up some more info about the and i found CFArrayContainsValue

BOOL CFArrayContainsValue(CFArrayRef theArray, CFRange range, const void *value);

Example:

NSArray *numbers;
NSNumber *value;
BOOL found = CFArrayContainsValue ( (__bridge CFArrayRef)numbers, 
                                    CFRangeMake(0, numbers.count), 
                                    (CFNumberRef)value );

Works like a charm and is really fast!

like image 141
Tieme Avatar answered Sep 17 '22 20:09

Tieme