What is deep copy and shallow copy
Shallow copy: Is the copy of the pointer, the copy of the pointer to the original pointer to the same location in memory of the object. As to whether the reference count value of the object is +1, it is to see if the variable assigned by the copy pointer is of type strong or week type. In the case of the strong type, the memory count value of the copied object is +1, and if assigned to a variable of type weak, the memory count value of the copied object remains inconvenient.
Deep copy: Copies a copy of the object in memory, puts it in a new position, and assigns the pointer to the new position to another variable.
The key to determining the type of copy is the address of the object that the pointer points to in memory.
There are two copies of the Copy method in iOS, one copy and the other is mutablecopy. As to whether these two methods are a ' shallow copy ' or ' deep copy ' of the resulting copy, it is up to the specific circumstances to decide.
1: Copy of non-container class object.
What is a non-container class object. Objects such as NSString, NSNumber, and so on, which can only store one value, are called non-container class objects.
1.1: Copy of non-container class immutable object
NSString *string = @ " non-Container class object ";
nsstring *stringcopy = [string copy];
nsmutablestring *mstringcopy = [string copy];
nsstring *stringmutcopy = [string mutablecopy];
nsmutablestring *mstringmutcopy = [string mutablecopy];
NSLog(@ " object's address:%p", String); Address of object:0x107d7e6d8
NSLog(@ " object's address:%p", stringcopy); address of object:0x107d7e6d8
NSLog(@ " object's address:%p", mstringcopy); address of object:0x107d7e6d8
NSLog(@ " object's address:%p", stringmutcopy); Address of object:0x7f9f397022e0
NSLog(@ " object's address:%p", mstringmutcopy); // object address:0x7f9f395043c0
Analysis by the above print results:
For non-container class immutable objects: Copy is a shallow copy, Mutablecopy belongs to a deep copy
Why is that?
After replication, iOS also takes into account the principle that memory controls and variables do not interfere with each other. For an immutable object in memory, because the object is immutable, for variables that use this object, pointing to the same memory location, which satisfies the use of the same content, and does not necessarily open up new memory space, achieves the purpose of saving memory space.
1.2: Copy of non-container class mutable object
nsmutablestring *string = [nsmutablestring stringwithstring:@ " non-container class mutable object "];
nsstring *stringcopy = [string copy];
nsmutablestring *mstringcopy = [string copy];
nsstring *stringmutcopy = [string mutablecopy];
nsmutablestring *mstringmutcopy = [string mutablecopy];
NSLog(@ " object's address:%p", String); Address of object:0x7fc2c9f054a0
nslog ( @ " Address of the object: %p" ,stringcopy); < span class= "s2" >// object address: 0x7fc2c9d08800
nslog ( @ " Address of the object: %p" ,mstringcopy); < span class= "s2" >// object address: 0x7fc2c9d168e0
nslog ( @ " Address of the object: %p" ,stringmutcopy); < span class= "s2" >// object address: 0x7fc2c9c065e0
NSLog(@ " object's address:%p", mstringmutcopy); // object address: 0x7fc2c9d179b0
You can see that the above objects have different addresses in memory, which means that the copies they take are all deep copies.
Summarize:
1: Copy of non-container class immutable object, copy method belongs to shallow copy, mutablecopy belongs to deep copy
2: Copy of non-container class mutable object, copy method belongs to deep copy, Mutablecopy also belongs to deep copy
This process is based on the principle of non-interference between objects and memory savings
2: Copy of Container class object
What is a container class object. is an object that can store other objects. such as: Nsarray,nsdictionary, etc.
2.1: Copy of Immutable container class object
Shallow copy and deep copy of iOS