Reputation: 899
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 the IndexBuffer
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?
Upvotes: 1
Views: 1215
Reputation: 6023
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)
Upvotes: 1
Reputation: 78363
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.
Upvotes: 2