Objective-c depth replication, objective-c depth Replication
Definition of deep replication:
Shallow copy: during replication, each layer of the Copied object is a pointer copy.
Deep replication: during replication, at least one layer of the Copied object is object replication.
Full replication: during replication, each layer of the Copied object is completely copied.
Retain.
Copy: for a variable object that is a deep copy, the reference count remains unchanged. For an immutable object, it is a shortest copy. If you add one to the referenced object, an immutable object is always returned.
MutableCopy: It is always a deep copy, and the reference count remains unchanged. A variable object is always returned.
Immutable object: The first address of the Memory changes as the value changes.
Variable object: no matter the value changes, the first address of the memory will not change.
Reference count: to let the user know how many owners the object has.
The following uses a program to demonstrate its usage:
1 # import <Foundation/Foundation. h> 2 3 int main (int argc, const char * argv []) {4 @ autoreleasepool {5 NSLog (@ "------------- conversion from an immutable object to an immutable object -----------"); 6 NSString * str1 = @ "aaaa "; 7 NSString * str2 = [str1 copy]; // For an immutable object, it is a potential copy, and the counter is incremented by one, always return an unchangeable object 8 NSLog (@ "str1 = % p, str2 = % p", str1, str2); // the address of str1 and str2 is the same, no new string 9 10 NSLog (@ "--------------- convert a mutable object to a mutable object -------------"); 11 NSMutableString * str3 = [NSMutableString stringWithString: @ "aaaa"]; 12 NSMutableString * str4 = [str3 mutableCopy]; // copy for a variable string is a deep copy of 13 NSLog (@ "str3 = % p, str4 = % p", str3, str4); // The str3 and str4 addresses are different, and a new string 14 15 NSLog is generated (@ "------------- convert a mutable object to an immutable object ---------------"); 16 str1 = [str3 copy]; // copy is a deep copy of 17 NSLog for variable characters (@ "str1 = % p, str3 = % p", str1, str3 ); 18 19 NSLog (@ "------------- transformation from an immutable object to a mutable object -------------"); 20 str3 = [str1 mutableCopy]; // copy 21 NSLog (@ "str1 = % p, str3 = % p", str1, str3); 22 23} 24 return 0 for immutable strings; 25}
Running result:
17:16:21. 062 test [2349: 183297]------------- Convert an immutable object to an immutable object -------------
17:16:21. 063 test [2349: 183297] str1 = 0x100001070, str2 = 0x100001070
17:16:21. 064 test [2349: 183297]------------- Convert a mutable object to a mutable object ---------------
17:16:21. 064 test [2349: 183297] str3 = 0x100200210, str4 = 0x1002003d0
17:16:21. 064 test [2349: 183297]------------- Convert a mutable object to an immutable object ---------------
17:16:21. 064 test [2349: 183297] str1 = 0x6161616145, str3 = 0x100200210
17:16:21. 064 test [2349: 183297]------------- Conversion from an immutable object to a mutable object ---------------
17:16:21. 065 test [2349: 183297] str1 = 0x6161616145, str3 = 0x1002002d0
When do I use deep copy?
Deep copy is used to convert an object from a mutable (immutable) to an immutable (immutable) or a copy of an inner part. The shortest is used to copy the pointer of an object.