Monday, June 17, 2013

NSIndexSet getIndexes example in Objective C (iOS).

NSIndexSet getIndexes

The index set fills an index buffer with the indexes contained both in the index set and in an index range, returning the number of indexes copied.

- (NSUInteger)getIndexes:(NSUInteger *)indexBuffer maxCount:(NSUInteger)bufferSize inIndexRange:(NSRangePointer)indexRangePointer

Parameters of [NSIndexSet getIndexes]
Index buffer to fill.
Maximum size of indexBuffer.
Index range to compare with indexes in the index set; nil represents all the indexes in the index set. Indexes in the index range and in the index set are copied to indexBuffer. On output, the range of indexes not copied to indexBuffer.

Return Value of [NSIndexSet getIndexes]
Number of indexes placed in indexBuffer.

Discussion of [NSIndexSet getIndexes]
You are responsible for allocating the memory required for indexBuffer and for releasing it later.

Suppose you have an index set with contiguous indexes from 1 to 100. If you use this method to request a range of (1, 100)—which represents the set of indexes 1 through 100—and specify a buffer size of 20, this method returns 20 indexes—1 through 20—in indexBuffer and sets indexRange to (21, 80)—which represents the indexes 21 through 100.

Use this method to retrieve entries quickly and efficiently from an index set. You can call this method repeatedly to retrieve blocks of index values and then process them. When doing so, use the return value and indexRange to determine when you have finished processing the desired indexes. When the return value is less than bufferSize, you have reached the end of the range.

NSIndexSet getIndexes example.
And this is how you can get the indexes as the elements of an array (represented by NSNumber objects):

NSIndexSet *set = // obtain the index set as above

NSUInteger size = set.count;

NSUInteger *buf = malloc(sizeof(*buf) * size);
[set getIndexes:buf maxCount:size inIndexRange:NULL];

NSMutableArray *array = [NSMutableArray array];

NSUInteger i;
for (i = 0; i < size; i++) {
    [array addObject:[NSNumber numberWithUnsignedInteger:buf[i]]];


Example of [NSIndexSet getIndexes].
- (IBAction)menuAction:(id)sender
NSIndexSet* selectedRowIndex=[tableView selectedRowIndexes];
unsigned int count=[selectedRowIndex count]; // nombre d'index
unsigned int indexBuffer[count]; // allocation tableau des index
NSRange range=NSMakeRange(0,[tableView numberOfRows]);
unsigned int numberOfIndexes =[selectedRowIndex getIndexes:indexBuffer
maxCount: count

unsigned int i;

unsigned int index=indexBuffer[i];
printf("index %d\n",index);
NSMutableDictionary* data=[dataArray objectAtIndex:index];
if([sender tag]==100) // bleu
[data setObject:[NSColor blueColor] forKey:@"couleur"];
if([sender tag]==101) // rouge
[data setObject:[NSColor redColor] forKey:@"couleur"];
if([sender tag]==102) //noir
[data setObject:[NSColor blackColor] forKey:@"couleur"];

[tableView setNeedsDisplay:YES];

End of NSIndexSet getIndexes example article.