views:

489

answers:

2

New to Objective C so pardon my stupidity,..

I am trying to retrieve my NSIndexSet by calling GetIndexes:MaxCount:IndexRange

Here is my code

    const NSUInteger arrayCount = picturesArray.count;
NSUInteger theIndexBuffer[arrayCount];  
[picturesArray getIndexes:theIndexBuffer maxCount:arrayCount inIndexRange:nil];

However, in the debugger theIndexBuffer is always showing as -1. When I initialize it with a static number like NSUInteger theIndexBuffer[10], I get a proper instance.

This is probably because it doesnt know what "arrayCount" is at compile time. What is the correct way of doing this?

+1  A: 

You can dynamically allocate it using a pointer like this:

NSUInteger *theIndexBuffer = (NSUInteger *)calloc(picturesArray.count, sizeof(NSUInteger));
// Use your array however you were going to use it before
free(theIndexBuffer); // make sure you free the allocation

In C, even though this is a dynamically allocated buffer, you can use the array subscript operators and (for the most part) pretend that it's just an array. So, if you wanted to get the third element, you could write:

NSUInteger thirdElement = theIndexBuffer[2]; // arrays are 0-indexed

Just don't forget to free it when you're done.

Jason Coco
A: 

I don't think you did anything wrong. It's valid in C99. You can double check by seeing if this evaluates as true:

sizeof(theIndexBuffer) == arrayCount * sizeof(NSUInteger)
Chris Lundie