Take the code below:
NSPredicate *predicateSource = [NSPredicate predicateWithFormat:@"SELF = %d",5];
NSArray *filteredArraySource = [[[self.myArray copy]autorelease] filteredArrayUsingPredicate:predicateSource];
myArray (NSMutableArray) contains 100 integers (numbers 1-100 in random order). What I’d like to do is somehow find out which index contains the number 5 without looping through each object in the array. The above code only extracts the object and places it into an array.
Any suggestions?
You can use
indexOfObjectPassingTest:. If you need to useNSPredicate, call your predicate’sevaluateWithObject:inside the block; otherwise, simply check the object’s integer value to be5.