Shimen the main contribution, respect the work of the author, please do not reprint.
If the article is helpful to you, you are welcome to donate to the author, support Shimen, and donate any amount. Heavy on the mind ^_^
I want to donate: Click Donate
Cocos2d-x source code Download: Dot I teleport
Assigning a variable to another object only creates a reference to the object.
Similar to Java, an assignment is a reference.
The reference to C + + or C pointers to understand is easy. The assignment here is actually the pointer address assignment.
The Foundation class implements a method called copy and mutablecopy that enables you to create a copy of an object using these methods.
Complete this task by implementing a method that conforms to the <NSCopying> protocol (for making a copy).
Note that generating a mutable copy of an object does not require that the copied object itself be mutable. , you can create immutable copies of Mutable objects.
Shallow copy and deep copy:
Mutablecopy method when copying an array. The new Array object is allocated space in memory. and a single element is copied into the new array.
Then copying each element in the original array to a new location means: Copy the reference from only one array element to another array element.
The result is that the elements in the two array all point to the same string in memory.
To create a completely different copy of each element in an array, you need to run a so-called deep copy.
Implementation <NSCopying> Agreement:
Attention. Suppose you want to differentiate between a mutable copy and an immutable copy.
It is also necessary to implement Mutablecopywithzone based on <NSMutableCoping> protocol: methods.
Assume that two methods are implemented. Then Copywithzone: The immutable copy should be returned.
Instead, Mutablecopywithzone: A mutable copy is returned.
Property does not have a mutablecopy attribute.
Even a mutable instance variable. It is also used with the copy feature, as the method Copywithzone: Operating performance.
So. By protocol, an immutable copy of an object is produced.
Objective-c Road "18-Copy Object"