0
votes

I'm relatively new to objective-C and I'm having a problem with a 2D NSMutableArray and it's behaviour when adding the contents of one NSMutableArray to another.

I have a singleton class containing an NSMutableArray. This array is for storing NSURL values. At a certain point in my program, I need to add this NSMutableArray as a whole to another NSMutableArray.

I do something like this: [arrayIWantStuffIn addObject: mySingleton.singletonMutableArray];

The problem I have is that I need to reset my singltonMutableArray to start storing a new set of NSURL values. The addObject function above appears to be passing by reference rather than passing by value, so when I do:

[mySingleton.singletonMutableArray removeAllObjects];

The data stored in arrayIWantStuffIn is wiped as well.

How can I achieve a 'deep' addObject that is adding a NSMutableArray to another NSMutableArray by value and not by reference?

1

1 Answers

0
votes

You dont actually need a deep copy just another mutableCopy. This can be done simply by doing the following

[arrayIWantStuffIn addObject: [[mySingleton.singletonMutableArray mutablCopy] autorelease]];

Once that is done when the singletonMutableArray is cleared or changed it will not change your items in arrayIWantStuffIn and you can still modify the contents independently.