1

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?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
user82383
  • 879
  • 3
  • 11
  • 31

2 Answers2

2

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
  • 77,985
  • 20
  • 184
  • 180
1

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
  • 6,023
  • 2
  • 27
  • 28