- A shallow copy, or pointer copy, can be thought of as a piece of rope for a dog; a deep copy not only copies the pointer of the object, but also allocates a piece of memory in the system to hold the contents of the Copy object, which can be thought of as producing a new dog with a rope. That is, a shallow copy is two ropes a dog, a deep copy is two rope two dogs and each dog corresponds to its own rope.
- Icon:
A deep copy depends on whether the copied object is the same as the address of the copied object, and if it is different, a new object is created, which is a dark copy. If different, it is just a pointer copy, which is equivalent to retain the original object, that is, a shallow copy.
Copy and Mutablecopy
The copy-copied object is an immutable object, and mutablecopy the copied object is a mutable object, regardless of whether the original object is mutable or not.
Copy and mutablecopy of non-collection classes
- A copy operation on an immutable object is a pointer copy (shallow copy), and performing a mutablecopy operation is a copy of the content (deep copy).
- Copy operations and Mutablecopy operations on mutable objects are content replication (deep copy).
The code is represented as follows:
[immutableObject copy]; // 浅复制[immutableObject mutableCopy]; // 深复制[mutableObject copy]; // 深复制[mutableObject mutableCopy] ; // 深复制
Shallow copy and deep copy of container classes
- A container object is similar to a non-container object, and a mutable object replicates a new object, the immutable object copy is a shallow copy, and the mutablecopy is a deep copy
- For a container, an element object is always a pointer copy.
Verification Code:
Nsmutablearray *array01 = [Nsmutablearray arraywithobjects:@ "a", @ "B", @ "C", nil]; Nsarray *copyarray01 = [array01 copy]; Nsmutablearray *mutablecopyarray01 = [Array01 mutablecopy]; NSLog (@ "array01 =%p,copyarray01 =%p", array01,copyarray01); NSLog (@ "array01 =%p,mutablecopyarray01 =%p", array01,mutablecopyarray01); NSLog (@ "=================================================================="); [Array01 addobject:@ "D"]; NSLog (@ "array01 =%p,copyarray01 =%p", array01,copyarray01); NSLog (@ "array01 =%p,mutablecopyarray01 =%p", array01,mutablecopyarray01); NSLog (@ "array01 =%@\n copyArray01 =%@\n mutableCopyArray01 =%@", array01,copyarray01,mutablecopyarray01); NSLog (@ "array01[0] =%p,array01[1] =%p,array01[2] =%p,array01[3] =%p", array01[0],array01[1],array01[2],array01[3]); NSLog (@ "copyarray01[0] =%p,copyarray01[1] =%p,copyarray01[2] =%p", copyarray01[0],copyarray01[1],copyarray01[2]); NSLog (@ "mutablecopyarray01[0] =%p,mutablecopyarray01[1] =%p,mutablecopyarray01[2] =%p", Mutablecopyarray01[0],mutaBLECOPYARRAY01[1],MUTABLECOPYARRAY01[2]); NSLog (@ "=================================================================="); [MutableCopyArray01 addobject:@ "E"]; NSLog (@ "array01 =%p,copyarray01 =%p", array01,copyarray01); NSLog (@ "array01 =%p,mutablecopyarray01 =%p", array01,mutablecopyarray01); NSLog (@ "array01 =%@\n copyArray01 =%@\n mutableCopyArray01 =%@", array01,copyarray01,mutablecopyarray01); NSLog (@ "array01[0] =%p,array01[1] =%p,array01[2] =%p", array01[0],array01[1],array01[2]); NSLog (@ "copyarray01[0] =%p,copyarray01[1] =%p,copyarray01[2] =%p", copyarray01[0],copyarray01[1],copyarray01[2]); NSLog (@ "mutablecopyarray01[0] =%p,mutablecopyarray01[1] =%p,mutablecopyarray01[2] =%p,mutablecopyarray01[3] =%p", MUTABLECOPYARRAY01[0],MUTABLECOPYARRAY01[1],MUTABLECOPYARRAY01[2],MUTABLECOPYARRAY01[3]);
Printing results:
2018-06-14 11:16:43.117758+0800 copy_demo[1988:95724] array01 = 0X604000443CF0,COPYARRAY01 = 0x604000443bd02018-06-14 11:16:43.117887+0800 copy_demo[1988:95724] Array01 = 0X604000443CF0,MUTABLECOPYARRAY01 = 0x604000443a802018-06-14 11:16:43.118014+0800 copy_demo[1988:95724] ================================================================== 2018-06-14 11:16:43.118108+0800 copy_demo[1988:95724] array01 = 0X604000443CF0,COPYARRAY01 = 0x604000443bd02018-06-14 11:16:43.118323+0800 copy_demo[1988:95724] Array01 = 0X604000443CF0,MUTABLECOPYARRAY01 = 0x604000443a802018-06-14 11:16:43.118485+0800 copy_demo[1988:95724] array01 = (a,b,c,d) copyArray01 = (a,b,c) mutableCopyArray01 = (a,b,c) 2018-06-14 11:16:43.118559+0800 copy_demo[1988:95724] array01[0] = 0x10bf06128,array01[1] = 0x10bf06148,array01[2] = 0X10BF06168,ARRAY01[3] = 0x10bf061e82018-06-14 11:16:43.118661+0800 copy_demo[1988:95724] copyArray01[0] = 0X10BF06128,COPYARRAY01[1] = 0x10bf06148,copyarray01[2] = 0x10bf061682018-06-14 11:16:43.118758+0800 copy_demo[1988:95724] mutablecopyarray01[0] = 0x10bf06128,mutablecopyarray01[1] = 0x10bf06148, MUTABLECOPYARRAY01[2] = 0x10bf061682018-06-14 11:16:43.118891+0800 copy_demo[1988:95724] ========================== ========================================2018-06-14 11:16:43.119034+0800 copy_demo[1988:95724] Array01 = 0x604000443cf0,copyarray01 = 0x604000443bd02018-06-14 11:16:43.119095+0800 copy_demo[1988:95724] Array01 = 0x604000443cf0,mutablecopyarray01 = 0x604000443a802018-06-14 11:16:43.119185+0800 copy_demo[1988:95724] array01 = (A, B,C,D) CopyArray01 = (a,b,c) mutableCopyArray01 = (a,b,c,e) 2018-06-14 11:16:43.119346+0800 copy_demo[1988:95724] Array01[0] = 0x10bf06128,array01[1] = 0x10bf06148,array01[2] = 0x10bf061682018-06-14 11:16:43.119527+0800 copy_demo[ 1988:95724] copyarray01[0] = 0x10bf06128,copyarray01[1] = 0x10bf06148,copyarray01[2] = 0x10bf061682018-06-14 11:16:43.119595+0800 copy_demo[1988:95724] mutablecopyarray01[0] = 0x10bf06128,mutablecopyarray01[1] = 0x10bf06148, MutabLECOPYARRAY01[2] = 0x10bf06168,mutablecopyarray01[3] = 0x10bf06288
Deep copy shallow copy frequently asked in the interview, this is just a simple understanding and summary, in addition to these, there are many more complex and custom object deep copy and shallow copy.
Recommend a more detailed and profound article: Https://github.com/pro648/tips/wiki/%E6%B7%B1%E5%A4%8D%E5%88%B6%E3%80%81%E6%B5%85%E5%A4%8D%E5 %88%b6%e3%80%81copy%e3%80%81mutablecopy. I also made reference to this article when I was finishing this article, thanks to the author for sharing.