iphonecobjective-cxcodensenumerator

How do I get the index of the current Object in an NSEnumerator iteration?


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...)


Solution

  • 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's getObjects:range: method.

    size_t count = [myArray count];
    id *objects = calloc(count, sizeof(id));
    
    [myArray getObjects:objects range:NSMakeRange(0, count)];
    
    // if you have a very large count, you'll save doing an
    // "objectAtIndex:" for each item.
    
    for (int i = 0; i < count; i++)
        [objects[i] doSomething];
    
    free(objects);
    

    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 returns YES from isEqual: message (which may include further message sending).