0

In my application I need to create a copy of NSMutableArray. Currently I am using mutableCopy method. But when I modify the copied array the original one is also modified. Please tell me How to create a new copy NSMutableArray. Here is the code

delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
 self.mainImagesArray = [[NSMutableArray alloc]initWithArray:[delegate.arrayForCarCaptureImages mutableCopy]];

and here i am modifying

UIImage *filteredImage =[[[[self.mainImagesArray objectAtIndex:i]valueForKey:valueVheck] objectAtIndex:j]  copy];
filteredImage =[filteredImage brightness:(1+sliderValue-0.5)];
[[[self.mainImagesArray objectAtIndex:i]valueForKey:valueVheck]removeObjectAtIndex:j];
[[[self.mainImagesArray objectAtIndex:i] valueForKey:valueVheck]insertObject:filteredImage atIndex:j];

After execution the arrayForCarCaptureImages also modified automatically.

Anupdas
  • 10,211
  • 2
  • 35
  • 60

5 Answers5

3

You need to create a deep copy. From what I understand, you're creating a copy of the NSMutableArray itself without making copies of it's individual elements.

From what I see in the code you've written:

  1. You're abusing the delegation pattern
  2. Your code is not readable. Try passing it along to another developer, I bet you'll get slapped very fast :)

Here's an example of a classic case of deep copying:

NSArray *numbersArr = @[@1,@2,@3];
NSMutableArray *numbersArrCopy = [NSMutableArray array];
for (NSNumber *num in numbersArr) {
    [numbersArrCopy addObject:[num copy]];
}

An easier approach:

NSArray *numbersArr = @[@1,@2,@3];
NSArray *numbersArrCopy = [[NSArray alloc] initWithArray:numbersArr copyItems:YES];

This is of course different than just:

NSArray *numbersArr = @[@1,@2,@3];
NSArray *numbersArrCopy = [numbersArr copy];
Stavash
  • 14,244
  • 5
  • 52
  • 80
  • 'self.mainImagesArray = [[NSMutableArray alloc]initWithArray:[delegate.arrayForCarCaptureImages mutableCopy]];' here Iam making copy of arrayForCarCaptureImages. and UIImage *filteredImage =[[[[self.mainImagesArray objectAtIndex:i]valueForKey:valueVheck] objectAtIndex:j] copy]; filteredImage =[filteredImage brightness:(1+sliderValue-0.5)]; [[[self.mainImagesArray objectAtIndex:i]valueForKey:valueVheck]removeObjectAtIndex:j]; [[[self.mainImagesArray objectAtIndex:i] valueForKey:valueVheck]insertObject:filteredImage atIndex:j]; modify – Suyash Sadh May 02 '13 at 08:55
  • Can you post an example for creating a deepCopy? – Anupdas May 02 '13 at 09:43
  • `- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag` – Daij-Djan May 02 '13 at 09:45
0

Have you tried:

NSArray *copiedArray = [[NSArray alloc] initWithArray:[otherArray copy]];
ingh.am
  • 25,981
  • 43
  • 130
  • 177
0

Try this code in your project:

NSMutableArray *array = [NSMutableArray arrayWithObjects:@"one", @"two", @"three", nil];

NSMutableArray *copiedArray = [array mutableCopy];


[array addObject:@"four"];
NSLog(@"copied %@", copiedArray);
Shams Ahmed
  • 4,498
  • 4
  • 21
  • 27
0

MutableCopy will not perform deep copy. You have to do it manually as given,

NSMutableArray *arrayCopy = [NSMutableArray array];
for (id element in arraySource)
    [arrayCopy addObject:[element mutableCopy]]; //or [arrayCopy addObject:[element copy]];
Augustine P A
  • 5,008
  • 3
  • 35
  • 39
0

You code is hardly readable. See if this is working

delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.mainImagesArray = [[NSMutableArray alloc]initWithArray:delegate.mainImagesArray 
                                                  copyItems:YES];


UIImage *filteredImage = self.mainImagesArray[i][valueVheck][j];
filteredImage = [filteredImage brightness:(1+sliderValue-0.5)];
[self.mainImagesArray[i][valueVheck]replaceObjectAtIndex:j 
                                              withObject:filteredImage];
Anupdas
  • 10,211
  • 2
  • 35
  • 60