written in front:
In a previously reprinted article, shallow copy and deep copy are explained in detail, along with the concept of deep copy (One-level-deep copy), full copy (true copy), and the idea that deep replication in iOS development is a one-layer deep assignment, This article will verify these concepts.
(single-layer and full-concept distinction: for example, multilayer arrays implement only one layer of content copy, and the other layer is a single-layer deep copy of the pointer copy; If multiple layers of content are implemented as copies are called fully assigned values)
Some of the concepts used in the program are supplemented
(1)
Shallow copy (shallow copy): During a shallow copy operation, the pointer is copied for each layer of the copied object.
Deep copy (One-level-deep copy): In a deep copy operation, at least one layer of the copied object is a deep copy.
Full replication (real-deep copy): During a full copy operation, object replication is for each layer of the replicated object.
(2)
The concept of archiving and documentation complements:
There are times when there is a need to save multiple objects and their attribute values that are used in the program, and their relationships to a file, or to another process. To achieve this, the foundation framework can archive multiple interrelated objects as binary files, and also restore the relationship of the objects from the binaries.
Archive: Package an object into a binary file. Nskeyedarchiver: Archiver
Solution: The reverse transform of the archive. Nskeyedunarchiver: Unlocking Device
So you can use archive and file to achieve full replication
Code Validation
//创建多层数组 NSArray *array = @[@1,@2]; NSArray *oldArray = @[@"xxxx",array]; //浅复制 NSArray *shallowArray = [oldArray copy]; //深复制 NSArray *oneDeepLevelArray = [oldArray mutableCopy]; //完全深复制,利用归档和解档的方式 NSArray *trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:[NSKeyedArchiver archivedDataWithRootObject:oldArray]];
NSLog(@"%p,%p,%p,%p",oldArray,shallowArray,oneDeepLevelArray,trueDeepCopyArray);
Output Result:
0x7fc7e9c1c0d0,0x7fc7e9c1c0d0,0x7fc7e9c0c5c0,0x7fc7e9c22ef0
From the above printed address can be seen:
Shallow copy is just a simple pointer assignment, pointing to memory is still the same;
Deep replication, and full deep replication, both implement the replication of the content, but whether it is implemented to replicate each layer of the copied object, verify by looking at the address of the second-level element of the multilayer array:
NSLog(@"shallow——%p,%p",oldArray[1][0],shallowArray[1][0]); NSLog(@"oneDeep——%p,%p",oldArray[1][0],oneDeepLevelArray[1][0]); NSLog(@"trueDeep——%p,%p",oldArray[1][0],trueDeepCopyArray[1][0]);
Output Result:
shallow——0xb000000000000012,0xb000000000000012 oneDeep——0xb000000000000012,0xb000000000000012 trueDeep——0xb000000000000012,0xb000000000000013
Conclusion
Shallow copy address is the same, there is no doubt
The deep copy address is also the same, indicating that the second layer element does not implement a content copy, confirming that deep replication in iOS only enables single-layer replication
Full replication addresses are different, which means that the archive solution method implements a full copy of each of its layers to achieve a content copy
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Knowledge point grooming verification for IOS shallow assignment, deep copy, and full replication (additional archive solution)