Question: How do I get the index of the current Object in an NSEnumerator iteration?
(I don't want to keep track of things using an integer counter or use a for loop due to speed reasons. I did it before I just cannot remember how I did it...)
Question: How do I get the index of the current Object in an NSEnumerator iteration?
(I don't want to keep track of things using an integer counter or use a for loop due to speed reasons. I did it before I just cannot remember how I did it...)
It is doubtful that using an integer counter in a
for
loop will cause speed problems. It is more likely to be slower to try and find the index of a given object from an enumerator than it is to just keep a record of the index yourself. If you want to bypass repeated message dispatch, have a look at NSArray'sgetObjects:range:
method.You'll probably only see a minimal performance difference for incredibly large arrays, but don't underestimate the optimisations under the hood. Even the documentation for
getObjects:range:
discourages using this technique for this purpose.NSArray's
indexOfObject:
will iterate over all the elements until one returnsYES
fromisEqual:
message (which may include further message sending).