简体   繁体   中英

How to convert objective C array to a C array?

I have NSArray in Objective C, which store int only. How can I convert it to C array? Thanks.

My Objective C array:

NSArray *myArray = [[NSArray alloc] initWithObject:@"1", @"4", @"8", nil];

Allocate a chunk of memory using malloc to hold enough of the items (in this case, NSString* s and iterate over the NSArray, adding each item (and retaining each item) into an appropriate position in the c array.

Don't forget in doing this, you will have to send a release message to each member in the c array when you're done with it, before you also have to free() the array itself.

I guess you cannot explicitly since an NSArray encapsulates a C array, but an array of pointers to the objects referenced. So the better you can get is to retrieve (in reality copy) that array of pointers. If you want to get the value of the objects you need to 'query' them for their values, have a look here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM