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?
The
mutableCopymethod performs “shallow” copy. Each element ofarrayAis a reference to an object that is also inarrayB. If you add elements toarrayA(or remove elements),arrayBwill be unchanged, and vice versa. But since the elements ofarrayAandarrayBreference 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:That will have this effect: