NSMutableArray. What is the fastest way to convert it to a plain ole C-array
-
06-07-2019 - |
Question
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
Solution
from cocoabuilder mailing list
id *buffer = malloc(numberOf_ItemsIWant_ToFetch * sizeof(id));
[myArray getObjects:buffer range:rangeOfItems]
OTHER TIPS
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.
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.
int i=0;
foreach(YourClass *obj in array)
{
carray[i++] = [obj yourMethodToConvertToCType];
}