How to create separate instance for NSMutableArray

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.


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

Need Your Help

Help convert this jQuery.getJson to jQuery.ajax()

jquery ajax

I need to reformat the function below in the form of an jQuery.ajax call in order to do error handling (the script is apparently timing out on some calls). What would this look like if converted to

Y axis scaling issue with GridLinePlot() in ReportLab

django reportlab

I have just spotted some weird behaviour with GridLinePlot() in a django