sooper
sooper

Reputation: 6039

Finding the index/location of an object inside an array without looping through all its contents

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?

Upvotes: 4

Views: 160

Answers (2)

Ingolmo
Ingolmo

Reputation: 575

You can use indexOfObject:.

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726519

You can use indexOfObjectPassingTest:. If you need to use NSPredicate, call your predicate's evaluateWithObject: inside the block; otherwise, simply check the object's integer value to be 5.

Upvotes: 1

Related Questions