Deep and light copy in objective-c, and objective-c copy
Shallow copy: only copies the pointer to the object, instead of copying the reference object itself. Counter + 1, like retain
Deep replication: Copies the referenced object itself. The counter remains unchanged and a new object is generated.
Retain: always light replication. Add one to each reference count. Whether the returned object is changeable is consistent with the Copied object.
Copy: For a mutable object that is a deep copy, the reference count does not change. For an immutable object, the reference count is incremented by one. Always Returns an unchangeable object.
MutableCopy: It is always a deep copy, and the reference count remains unchanged. Always Returns a mutable object.
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {NSMutableArray * array1 = [NSMutableArray arrayWithObjects: @ "two day", nil]; NSMutableArray * array2 = [array1 copy]; NSArray * array3 = [array2 copy]; NSArray * array4 = [array2 retain]; NSMutableArray * array5 = [array1 retain]; NSMutableArray * array6 = [array1 mutableCopy]; NSArray * array7 = [array2 mutableCopy]; NSLog (@ "-------------"); NSLog (@ "array1: % @ % p", array1, array1); NSLog (@ "array2: % @ % p", array2, array2); NSLog (@ "array3: % @ % p", array3, array3 ); NSLog (@ "array4: % @ % p", array4, array4); NSLog (@ "array5: % @ % p", array5, array5); NSLog (@ "array6: % @ % p ", array6, array6); NSLog (@" array7: % @ % p ", array7, array7); NSLog (@" ------------- "); return 0 ;}Source code
20:46:23. 131 04-01-person [2204: 165655] ------------- 20:46:23. 132 04-01-person [2204: 165655] array1 :( "two day") 0x1001145702015-04-01 20:46:23. 133 04-01-person [2204: 165655] array2 :( "two day") 0x1001147002015-04-01 20:46:23. 133 04-01-person [2204: 165655] array3 :( "two day") 0x1001147002015-04-01 20:46:23. 133 04-01-person [2204: 165655] array4 :( "two day") 0x1001147002015-04-01 20:46:23. 133 04-01-person [2204: 165655] array5 :( "two day") 0x1001145702015-04-01 20:46:23. 133 04-01-person [2204: 165655] array6 :( "two day") 0x1001150d02015-04-01 20:46:23. 134 04-01-person [2204: 165655] array7 :( "two day") 0x100115b002015-04-01 20:46:23. 134 04-01-person [2204: 165655] ------------- Program ended with exit code: 0Running result
Array type conversion:
1. immutable object → variable object conversion:
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {NSArray * array01 = [NSArray arrayWithObjects: @ "zhang", @ "wang", @ "li ", nil]; NSMutableArray * array02 = [array01 mutableCopy]; // mutableCopy always returns a variable object NSLog (@ "array01: % @ % p", array01, array01 ); NSLog (@ "array02: % @ % p", array02, array02); return 0 ;}Source code 21:00:28. 218 04-01-person [2252: 170414] array01: (zhang, wang, li) 0x10020.c8021:00:28. 220 04-01-person [2252: 170414] array02: (zhang, wang, li) 0x1001076d0Program ended with exit code: 0Running result
2. Variable object → non-variable object conversion:
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {NSMutableArray * array1 = [NSMutableArray arrayWithObjects: @ "zhang", @ "wang", @ "li ", nil]; NSArray * array2 = [array1 copy]; // copy does not change the reference count if the variable object is deep copy. For immutable objects, the reference count is added to one at a time. Always return an unchangeable object NSLog (@ "array1: % @ % p", array1, array1); NSLog (@ "array2: % @ % p", array2, array2 ); return 0 ;}Source code 21:03:45. 349 04-01-person [2263: 171615] array1: (zhang, wang, li) 0x1001129002015-04-01 21:03:45. 350 04-01-person [2263: 171615] array2: (zhang, wang, li) 0x1001133b0Running result
3. Variable object → variable object conversion (different pointer variables point to different memory addresses ):
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {NSMutableArray * array1 = [NSMutableArray arrayWithObjects: @ "zhang", @ "wang", @ "li ", nil]; NSMutableArray * array2 = [array1 mutableCopy]; NSLog (@ "array1: % @ % p", array1, array1); NSLog (@ "array2: % @ % p ", array2, array2); return 0 ;}Source code 20:57:41. 732 04-01-person [2243: 169571] array1: (zhang, wang, li) 0x1001145702015-04-01 20:57:41. 734 04-01-person [2243: 169571] array2: (zhang, wang, li) 0x100115020Program ended with exit code: 0Running result
4. pointer replication between objects of the same type (different pointer variables point to the same memory address ):
# Import <Foundation/Foundation. h> int main (int argc, const char * argv []) {NSArray * array1 = [NSArray arrayWithObjects: @ "one", @ "two", @ "three ", nil]; NSArray * array2 = [array1 copy]; NSLog (@ "array1: % @ % p", array1, array1); NSLog (@ "array2: % @ % p ", array2, array2); return 0 ;}Source code 20:53:07. 398 04-01-person [2217: 167996] array1: (one, two, three) 0x1006069a02015-04-01 20:53:07. 399 04-01-person [2217: 167996] array2: (one, two, three) 0x1006069a0Program ended with exit code: 0Running result
When multiple pointers direct to the same memory area at the same time, these pointers also have ownership of the memory area. In this case, a shortest copy is required.