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.