Showing posts with label getIndexes example. Show all posts
Showing posts with label getIndexes example. Show all posts

Wednesday, June 19, 2013

NSIndexPath getIndexes example in Objective C (iOS).


NSIndexPath getIndexes

Copies the objects contained in the index path into indexes.

- (void)getIndexes:(NSUInteger *)indexes

Parameters of [NSIndexPath getIndexes]
indexes
Pointer to a C array of objects of size at least the length of the index path. On return, the index path’s indexes.

Discussion of [NSIndexPath getIndexes]
It is the developer’s responsibility to allocate the memory for the C array.

NSIndexPath getIndexes example.
You have to allocate the NSUInteger array of size [indexPath length] and pass it as argument. The return value will be written there. You have to release that array yourself or do nothing it was created on stack like this:

NSUInteger array[[indexPath length]];
[indexPath getIndexes: array];

Example of [NSIndexPath getIndexes].
NSIndexPath *someIndexPath = [NSIndexPath indexPathWithIndexes:newIndexes
                                                        length:len];
    NSUInteger newIndexes2[[someIndexPath length]];
    [someIndexPath getIndexes:newIndexes2];

    NSUInteger len2 = sizeof(newIndexes2)/sizeof(NSUInteger);
    NSIndexPath *baseIndexPath2 = [NSIndexPath indexPathWithIndexes:newIndexes2
                                                             length:len2];
    NSUInteger ii;
    NSInteger start = [someIndexPath indexAtPosition:[someIndexPath length] -1];
    for (ii=start; ii<10; ii++) {
        NSIndexPath *newIndexPath2 = [baseIndexPath2 indexPathByAddingIndex:ii];
        NSLog(@"newIndexPath2 = %@", newIndexPath2);
    }

End of NSIndexPath getIndexes example article.

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]
indexBuffer
Index buffer to fill.
bufferSize
Maximum size of indexBuffer.
indexRange
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]]];
}

free(buf);

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
inIndexRange:&range];

unsigned int i;

for(i=0;i<numberOfIndexes;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.