mutable copy copies by reference, not value?

nsmutablearray, objective-c

Solution

The `mutableCopy` method performs “shallow” copy. Each element of `arrayA` is a reference to an object that is also in `arrayB`. If you add elements to `arrayA` (or remove elements), `arrayB` will be unchanged, and vice versa. But since the elements of `arrayA` and `arrayB` reference the same objects, a change to one of those objects “shows up” in both arrays.

If you want a one-level deep copy of `arrayB`, you can do this:

NSMutableArray *arrayA = [[NSMutableArray alloc] initWithArray:arrayB copyItems:YES];

That will have this effect:

NSMutableArray *arrayA = [[NSMutableArray alloc] init];
for (id element in arrayB) {
    [arrayA addObject:[element copy]]; //copies immutable objects to new array  
}

Problem

Apparently mutableCopy copies by reference, not value. Ie if I do this: ``` NSMutableArray arrayA = [arrayB mutableCopy]; ``` then change values of arrayB, then arrayA's values will also be changed. I think Java has a clone() method to copy by value.. is there an equivalent in objective c?

Original source