Who can tell me the difference between assignment and copy of NSArray?
1.Assignment
NSArray* a = xxxxxx; NSArray* b; b = a;
2.COPY
NSArray* a = xxxxxx;
NSArray* b;
b = [a copy];
I know that a copy is a "light copy", a mutable copy is a "deep copy".
copy is the address of the memory copy, and the modified copy is the memory copy objects.
But I do not know the purpose and am copying another.
Same?
source to share
This Apple Documentation is helpful:
A regular copy is a shallow copy that creates a new collection that shares ownership of the objects with the original. Deep Copies create new objects from the originals and add them to a new collection.
Therefore, in the first example it b
points to the same NSArray instance as a
. The second example b
points to a new NSArray instance containing references to the same objects contained in the array it points to a
.
source to share
b = a;
b
indicates the same address where it a
indicates. So changing one will be the same. It's called how call by reference
.
b = [a copy];
creates another object a
and points to b
. Two different sets of objects are formed here, so changing one of them will be independent of each other. It looks like call by value
. And here b
will contain immutableCopy a
even if a
changed.
source to share