2

I am having hard time with a simple array that i want to pass .

I have a class with some NSMutableArray that i pass to another class(the array is global from singleton)

 [mgzm saveWithArray:[Globals sharedGlobals].allImages];

To this function :

-(void)saveWithArray:(NSMutableArray*)currentArray
{

     dataToSave=[[NSMutableArray alloc] init]; //local
     dataToSave=[currentArray mutableCopy]; //copy

Than i saw that is is changing the original array which i don't want . So i did this :

dataToSave=[[NSMutableArray alloc] initWithArray:currentArray copyItems:YES];

Which result in a situation that i can't change the dataToSave array get get a crash when trying to.(it needs to be changed).

Than i did this :

for(NSMutableDictionary *dic in currentArray)
    [dataToSave addObject:dic];

Which again if i change dataToSave it change also the original array (?! )

Is there a way in this language to COPY array without changing the original one ????

2 Answers2

2

When you make a copy of a mutable array, changing the array copy does not change the original array, not the objects inside the array. Here is what happens when you call [currentArray mutableCopy]:

What you have

The two arrays are pointing to the same objects. If you remove an object from the copy, the original array would still have it. However, if you modify the object itself (say, change the name of A to X) the change will reflect on the object in the original array, because it is the same object.

Here is what you want to happen:

What you want

Now the two arrays are completely independent of each other. This is the effect that you achieve when you call

dataToSave=[[NSMutableArray alloc] initWithArray:currentArray copyItems:YES];

However, there is a catch: in order for this to work, the objects inside the array must conform to NSCopying protocol, otherwise the code is going to crash.

To fix this, make sure that the objects inside NSMutableArray implement NSCopying. Here is an answer that explains how it is done.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Wow this is the greatest answer i have seen so far here . and it did crash because i had UIImages inside the dictionary . how can i save this answer ? –  May 31 '14 at 11:11
  • 1
    @user1994291 Thank you! You can save the answer by adding the question to favorites (click a small asterisk next to the question). – Sergey Kalinichenko May 31 '14 at 11:17
0

The problem is although you are creating a new array the NSDictionary objects within the new array are still the same ones. So you need to make copies of the NSDictionary objects, you we're close but you need to do something like this...

-(void)saveWithArray:(NSMutableArray*)currentArray
{

     dataToSave=[[NSMutableArray alloc] init]; //local
     for(NSMutableDictionary *dic in currentArray)
           [dataToSave addObject:[NSDictionary dictionaryWithDictionary:dic];
}
Flexicoder
  • 8,251
  • 4
  • 42
  • 56
  • oh great thanks. but why the first 2 copies i made also not works and change the original values? –  May 31 '14 at 10:12
  • because again its just making a copy of the array, not an actual copy of the `NSDictionary` objects within. Where as this approach is actually creating new `NSDictionary` objects – Flexicoder May 31 '14 at 10:19