Issue related with modifying the value in an NSMutablearray

64 Views Asked by At

I am facing an issue with the values in NSMutablearray. I have two NSMutablearray, both are holding the same content using mutablecopy. The issue is that when I modify a value in one array, the corresponding value in the second array also get modified. How to resolove this. Please help me.

1

There are 1 best solutions below

0
On BEST ANSWER

mutableCopy copies by reference, not value. So, any change to one of those objects affects for both arrays.

You could realize different methods to overcome this situation.

// first method

nameArray2 = [NSMutableArray new];
[nameArray2 addObjectsFromArray:nameArray1];

// second method

nameArray2 = [[NSMutableArray alloc] initWithArray:nameArray1 copyItems:YES];

Best regards.