Search code examples
objective-ciosxcodensarraynsindexset

Finding the nth entry in an nsindexset


Say i have an NSIndexSet called aSet with numbers ranging from 26-89.

[aSet firstIndex] gives me 26

[aSet lastIndex] gives me 89

What method would give the 3rd index (29)?


Solution

  • I don't think NSIndexSet can be used to access like that. You'll need to iterate through it. For instance -

    int indexIwantToFind = 2;
    int valueAtThisIndex = [aSet firstIndex];
    for(int i = 0; i < indexIwantToFind; i++){
        valueAtThisIndex = [aSet indexGreaterThanIndex:valueAtThisIndex];
    }
    NSLog(@"%d", valueAtThisIndex); //This will give you 39