1. Concept Summary:
A shallow copy is a copy of the pointer to the original object, and the reference count of the original object is +1. A shallow copy is a copy of the pointer to the original object, and the reference count of the original object is +1. A shallow copy is a copy of the pointer to the original object, and the reference count of the original object is +1.
1.2. Deep copy refers to copying the specific contents of the object, and the memory address is allocated independently. After the copy, the two objects save the same value, but the memory address is different, and the two objects do not affect each other.
Summary: the shallow copy copies the pointer, the shallow copy reference count is increased by 1; A deep copy copies memory and creates new objects with new addresses.
2, container, non-container, mutable, immutable object copy and mutableCopy analysis
2.1, non-container objects – NSString, NSMutableString for example, first define NSString and NSMutableString, then create a string object for copy and mutableCopy. The results were regular and strange.
As can be seen in the picture above:
**1, execute the copy method on the immutable object, the memory address is not changed, and the shallow copy operation is performed; ** 2, execute a mutableCopy method on an immutable object, and the memory address is changed. 3. No matter copy or mutableCopy is executed for the mutable object, the memory address changes and deep copy operation is carried out; 4. If a string is created by a direct constant assignment, then the object of the string is stored in the constant area of type NSCFConstantString. The address of the string object created by the same string constant is the same.
2.2, container objects – For example, NSArray and NSMutableArray, create NSArray and NSMutableArray arrays respectively, fill in the immutable string item0 and mutable string Item1, and observe the printed results.
As summarized in the figure above:
**1, execute the copy method on the immutable container, the memory address is not changed, the shallow copy operation is performed; ****2, execute mutableCopy method on the immutable container, memory address changes, is a deep copy operation; ****3, for the mutable container no matter execute copy or mutableCopy method, memory will change, both are deep copy operation; ****4, the array returned by copy is of type __NAArrayI, and the array returned by mutableCopy is of type __NSArrayM because: An empty array is of type __NSArray0, an array with only one object is of type __NSSingleObjectArrayI, an array with objects is of type __NSArrayI, and a mutable array is of type __NSArrayM. ****5. Whether mutable or immutable, whether copy or mutableCopy is executed, item0 and Item1 do not change. The shallow copy is made. **6, so mutableCopy returns an array that is mutable, while copy returns an array that is immutable
3, about container single copy and multilayer copy
3.1. For container objects, a deep copy is not strictly “deep copy”. Although a new memory address is created, the elements of the newly created array memory are the same as those of the original array. This is called a “single layer deep copy.”
MArr1 [1], mArr2[1], mArr3[1], mArr2[1], and mArr3[1] are the same. When printed, item1 in mArr1, mArr2, and mArr3 is changed, which means that the addresses of the array elements are the same even though the addresses of the array elements are different. When you modify item1, you are modifying the data in the same memory address.
3.2. Implement full deep copy of collection objects (full deep copy)
The first method: archive file to achieve a full deep copy
The address of mArr4[1] is different from that of mArry1, the address of mArr4[1] is different from that of mArr1[1], and the value of Item1 is changed in line 36, but it does not affect Item1 of mArr4[1], so we can see that a full deep copy was made during the archiving process.
The second method: use the array initialization method to tell the array that copyItems is required to copy the elements
The address of mArr5[1] is different from that of mArry1, the address of mArr5[1] is different from that of mArr1[1], and the address of mArr5[1] is different from that of mArr1[1]. In line 39, we changed the value of Item1, but did not affect item1 of mArr5[1].