Objective C- How to get index of an object in the Array Objective C- How to get index of an object in the Array objective-c objective-c

Objective C- How to get index of an object in the Array


indexOfObject:
indexOfObjectIdenticalTo:

Edit:
Read carefully the descriptions, indexOfObjectIdenticalTo: is probably what you want in your case.
However, adding a counter is the fast way in this case.


What's wrong with the indexOfObject: method?

NSUInteger i = [ array indexOfObject: anObject ];


If you don't want a counter in your for loop, instead use NSMutableArray enumerateObjectsUsingBlock. This will iterate the elements and give you the index as an argument in the block you provide. Doing this will be a lot more performant that indexOfObject - which has to search the entire array before returning an index. It seems silly to use when you already 'know' the index.