views:

721

answers:

4

I like the convenience of NSMutableArray but sometimes you just need to drop down to good ole C-arrays. Like when you are feeding interleaved vertex arrays to OpenGL.

What is the fastest way of copying the contents of an NSMutableArray to a C-array?

Yah, I realize this bit shuffling introduces inefficiency but I'd like to see if I can sneak by with this approach without taking a hit in frame rate.

Cheers, Doug

+1  A: 
NSMutableArray *arrayOfThings;
Element elements[arrayOfThings.count+1];

for (int i=0;i<arrayOfThings.count;i++)
{
    elements[i] = [arrayOfThings objectAtIndex:i];
}

Or something similar. I don't see any easy way to do this in the Apple docs.

Dan Lorenc
+6  A: 

See -[NSArray getObjects:]. Just give it a buffer of the appropriate size and NSArray will fill the buffer with its contents. NSMutableArray is a subclass of NSArray, so it responds to all the same methods.

Chuck
+1  A: 
int i=0;
foreach(YourClass *obj in array)
{
   carray[i++] = [obj yourMethodToConvertToCType];
}
porneL
Thanks for all the answers gang. Super quick. Super helpful.
dugla
+6  A: 

from cocoabuilder mailing list

id *buffer = malloc(numberOf_ItemsIWant_ToFetch * sizeof(id));
[myArray getObjects:buffer range:rangeOfItems]
slf