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.

5 Answers 5

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];
Sign up to request clarification or add additional context in comments.

3 Comments

'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
Can you post an example for creating a deepCopy?
- (id)initWithArray:(NSArray *)array copyItems:(BOOL)flag
0

Have you tried:

NSArray *copiedArray = [[NSArray alloc] initWithArray:[otherArray copy]];

Comments

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);

Comments

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]];

Comments

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.