Reputation: 17581
In my code, I must to store an array inside another array: What's the best way?
first:
NSArray *arrayTemp = myArray;
second:
NSMutableArray *arrayTemp = [[NSMutableArray alloc]init];
[arrayTemp addObjectsFromArray:myArray];
...instruction....
[arrayTemp release];
Upvotes: 1
Views: 2749
Reputation: 25144
By doing arrayTemp = myArray
, you declare arrayTemp
as a new pointer to your existing array myArray
. That's not a copy (if you put X in myArray[42], arrayTemp[42] will also be X).
The second variant looks like you're doing a copy of your array, but still the array's values are copied by reference (by pointer), when you seem to need a copy "by value".
What you should try is simply:
NSArray *arrayCopy = [myArray copy];
Beware: from a memory management point of view, this is equivalent to a retain
or a alloc/init
: you should release your arrayCopy
after use.
Upvotes: 3