user7865437
user7865437

Reputation: 812

NSIndexPath getIndexes method....how?

Am confused with the use of this method and the documentation that lists it as a (void) method.

"on return the index path's indexes"

where does it return anything too?

Should it not be:

- (NSIndexPath *)getIndexes:(NSUInteger *)indexes

getIndexes: Provides a reference to the index path’s indexes.

- (void)getIndexes:(NSUInteger *)indexes

Parameters indexes Pointer to an unsigned integer array. On return, the index path’s indexes. Availability Available in iOS 2.0 and later. Declared In NSIndexPath.h

Upvotes: 3

Views: 2217

Answers (4)

user7865437
user7865437

Reputation: 812

Max, Thomas and Caleb, I have tried all three ways and cannot get anything to work, so maybe fired off the accepted solution too quick.....but probably more likely I just don't get it? I can't get the right size of the array in order to loop through it to access the required rows in my table. I would have though that calloc([indexPath length], sizeof(NSUInteger)) would for a group with 5 rows return an array with 5 rows with each row holding NSUIntegr.....or am of so far off beam it embarrassing?

Upvotes: 0

Max
Max

Reputation: 16709

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];

Upvotes: 4

Caleb
Caleb

Reputation: 125037

You send that message to an instance of NSIndexPath, so getting one back wouldn't help. The -getIndexes: method fills the array 'indexes' with the indexes from the index path. So you'd do something like:

NSUInteger *indexes = calloc([indexPath length], sizeof(NSUInteger));
[indexPath getIndexes:indexes];

After that, indexes will be filled with the index values that are in indexPath.

Upvotes: 0

Tomas Vana
Tomas Vana

Reputation: 18815

Maybe the next sentence explains the reason

It is the developer’s responsibility to allocate the memory for the C array.

It's actually a pointer to a C array that will be filled for you with the indexes, so there's no reason to additionally return it from the function - you already know its address.

You can use the function as follows

NSUInteger indexCount = [indices count];
NSUInteger buffer[indexCount];
[indices getIndexes:buffer maxCount:indexCount inIndexRange:nil];

Upvotes: 2

Related Questions