NSIndexSetEnumerator

description

I made a little class to help me around some of my confusions regarding NSIndexSet. This class allows you to enumerate through an NSIndexSet using the Apple-suggested method  :-

(unsigned int)getIndexes:(unsigned int *)indexBuffer
                maxCount:(unsigned int)bufferSize
                inIndexRange:(NSRangePointer)range

using simple enumerator-like messages.

Currently it doesn't support buffering (ie. if you have an NSIndexSet with one million entries, you'll end up with an internal array of one million entries), but it seems to work ok for what it is.

download

Please note that this is NOT rigorously tested or supported code, I just thought I'd share something that made my life a little easier (note also the extreme lack of code comments :-) ) but the code is relatively self explanatory (I hope!).

Download (4kb) :-  NSIndexSetEnumerator.tgz

code sample

A basic code sample using a while statement :-

unsigned int current_index;
NSIndexSet  *selectedItems = [theView selectedRowIndexes];
NSIndexSetEnumerator  *e = [NSIndexSetEnumerator
allocWithIndexSet: selectedItems];


while ([e nextIndex:&current_index])
{
   // do something extremely exciting with 'current_index'
}

and another, using a for loop :-

unsigned int current_index, loop_count;
NSIndexSet  *selectedItems = [theView selectedRowIndexes];
NSIndexSetEnumerator  *e = [NSIndexSetEnumerator allocWithIndexSet:selectedItems];

for (loop_count = 0; loop_count < [e count]; loop_count++)
{
    current_index = [e indexAtIndex:loop_count];

    // do something extremely exciting with 'current_index'
}
1