0

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];
2
  • 1
    You are not storing int but NSString representing integers to be precise Commented Sep 6, 2010 at 11:26
  • Furthermore, you can't store int values in an NSArray -- you need to store objects, like NSNumber Commented Sep 9, 2010 at 7:10

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.