newArary = oldArray
isn't a copy at all. You end up with two pointers pointing to the exact same memory location.
newArray = [NSMutableArray arrayWithArray:oldArray];
is a shallow copy. You end up with two distinct arrays, so if you were to remove or add items from one array, it wouldn't affect the other array. However, the items in the two arrays are identical. If the first element of oldArray
were an NSMutableDictionary
and you added a key to it, you'd see that change on the first element of newArray
as well (since those two objects are the same).
To do a deep copy, you would have to make a new array, and each element of the new array would be a deep copy of the corresponding element of the old array. (Yes, that definition is recursive).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…