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];
By doing
arrayTemp = myArray, you declarearrayTempas a new pointer to your existing arraymyArray. 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:
Beware: from a memory management point of view, this is equivalent to a
retainor aalloc/init: you should release yourarrayCopyafter use.