IOS images and text show you deep copy and light copy, ios images and text copy

Source: Internet
Author: User

IOS images and text show you deep copy and light copy, ios images and text copy
I. Concepts and summary

1. Shallow copy

Shallow copy refers to the replication of the memory address, so that the target object pointer and source object point to the same memory space. When the memory is destroyed, several pointers pointing to this memory need to be redefined before it can be used, otherwise it will become a wild pointer.

A shallow copy is a pointer to the original object, so that the reference count of the original object is + 1. It can be understood that a new pointer pointing to the original object is created, no new object is created.

2. Deep copy

Deep copy refers to the specific content of the object to be copied, and the memory address is allocated independently. After the copy is completed, although the two objects have the same storage value, the memory address is different, the two objects do not affect each other and do not interfere with each other.

Deep copy is to copy a new object that is just the same as the original value but has a completely different memory address. It has nothing to do with the original object after it is created.

3. Summary:

Deep copy means content copy, and shallow copy means pointer copy. The essential difference lies in:

  • Whether to enable the new memory address
  • Whether the reference count of the memory address is affected
Ii. Example Analysis

In iOS, deep copy and shallow copy are more complex, involving copying and mutableCopy of containers and non-containers, mutable and immutable objects. The following example is used for analysis:

1,Copy and mutableCopy of non-set objects

1.1 immutable object NSString

-(Void) noMutableNSStringTest {NSString * str1 = @ "test001"; NSMutableString * str2 = [str1 copy]; // copy returns an immutable object and str2 cannot be modified, therefore, a crash occurs. // [str2 appendString: @ "test"]; NSMutableString * str3 = [str1 mutableCopy]; [str3 appendString: @ "modify"]; NSLog (@ "str1: % p-% @ \ r \ n", str1, str1); NSLog (@ "str2: % p-% @ \ r \ n ", str2, str2); NSLog (@ "str3: % p-% @ \ r \ n", str3, str3 );}

Print result:

2017-07-20 18:02:10.642 beck.wang[1306:169414] str1:0x106abdbd0 - test001 2017-07-20 18:02:10.643 beck.wang[1306:169414] str2:0x106abdbd0 - test001 2017-07-20 18:02:10.643 beck.wang[1306:169414] str3:0x608000260940 - test001modify 

Analysis: The str1 and str2 addresses are the same and different from the str3 addresses. NSString copy is a shortest copy and the objects returned by copy are immutable objects. mutableCopy is a deep copy.

1.2 variable object NSMutableString

-(Void) mutableNSStringTest {NSMutableString * mstr1 = [NSMutableString stringWithString: @ "test002"]; NSMutableString * mstr2 = [mstr1 copy]; // copy returns an immutable object, mstr2 cannot be modified, so it will crash // [str2 appendString: @ "test"]; NSMutableString * mstr3 = [mstr1 mutableCopy]; [mstr3 appendString: @ "modify"]; NSLog (@ "mstr1: % p-% @ \ r \ n", mstr1, mstr1); NSLog (@ "mstr2: % p-% @ \ r \ n ", mstr2, mstr2); NSLog (@ "mstr3: % p-% @ \ r \ n", mstr3, mstr3 );}

Print result:

2017-07-20 18:14:35.789 beck.wang[1433:180881] mstr1:0x610000075e40 - test002 2017-07-20 18:14:35.790 beck.wang[1433:180881] mstr2:0xa323030747365747 - test002 2017-07-20 18:14:35.790 beck.wang[1433:180881] mstr3:0x610000074480 - test002modify 

Analysis: the addresses of mstr1, mstr2, and mstr3 are different. NSMutableString objects copy and mutableCopy are both deep copies, and the objects returned by copy are immutable objects.

 

2. copy and mutableCopy of collection objects

2.1 immutable object NSArray

- (void) mutableNSArrayTest{    NSArray *arry1 = [[NSArray alloc] initWithObjects:@"value1", @"value2",nil];        NSArray *arry2 = [arry1 copy];    NSArray *arry3 = [arry1 mutableCopy];        NSLog(@"arry1:%p - %@ \r\n",arry1,arry1);    NSLog(@"arry2:%p - %@ \r\n",arry2,arry2);    NSLog(@"arry3:%p - %@ \r\n",arry3,arry3);}

Print result:

2017-07-20 18:33:53.707 beck.wang[1502:194476] arry1:0x60800003b480 - (    value1,    value2) 2017-07-20 18:33:53.708 beck.wang[1502:194476] arry2:0x60800003b480 - (    value1,    value2) 2017-07-20 18:33:53.708 beck.wang[1502:194476] arry3:0x60800004cd20 - (    value1,    value2) 

Analysis: the addresses of arry1 and arry2 are the same. The addresses of arr3 are different. NSArray's copy is a shortest copy, and the objects returned by copy are immutable objects. mutableCopy is a deep copy.

2.2 variable object NSMutableArray

-(Void) implements {NSMutableArray * marry1 = [[NSMutableArray alloc] Using: @ "value1", @ "value2", nil]; NSMutableArray * marry2 = [marry1 copy]; // copy returns an immutable object and marry2 cannot be modified, so it will crash // [marry2 addObject: @ "value3"]; NSMutableArray * marry3 = [marry1 mutableCopy]; NSLog (@ "marry1: % p-% @ \ r \ n", marry1, marry1); NSLog (@ "marry2: % p-% @ \ r \ n ", marry2, marry2); NSLog (@ "marry3: % p-% @ \ r \ n", marry3, marry3 );}

Print result:

2017-07-20 18:55:43.243 beck.wang[1577:204641] marry1:0x600000048d60 - (    value1,    value2) 2017-07-20 18:55:43.244 beck.wang[1577:204641] marry2:0x600000026000 - (    value1,    value2) 2017-07-20 18:55:43.244 beck.wang[1577:204641] marry3:0x6000000494b0 - (    value1,    value2) 

Analysis: the addresses of marry1, marry2, and marr3 are different. NSMutableArray objects copy and mutableCopy are both deep copies, and the objects returned by copy are immutable objects.

 

Note that: For variable objects in the Collection class, deep copy is not strictly related to deep copy.Single-layer Deep ReplicationThat is, although the memory address is newly opened, but the value stored in the memory (that is, the element in the array is still the element value of the home Member array, and no other copy ), this is called Single-layer Deep replication.

Example:

-(Void) callback {NSMutableArray * marry1 = [[NSMutableArray alloc] init]; NSMutableString * mstr1 = [[NSMutableString alloc] initWithString: @ "value1"]; NSMutableString * mstr2 = [[NSMutableString alloc] Comment: @ "value2"]; [marry1 addObject: mstr1]; [marry1 addObject: mstr2]; optional * marry2 = [marry1 copy] NSMutableArray * marry3 = [marry1 mutableCopy]; NSLog (@ "marry1: % p-% @ \ r \ n", marry1, marry1); NSLog (@ "marry2: % p-% @ \ r \ n ", marry2, marry2); NSLog (@" marry3: % p-% @ \ r \ n ", marry3, marry3 ); NSLog (@ "array element address: value1: % p-value2: % p \ r \ n", marry1 [0], marry1 [1]); NSLog (@ "array element address: value1: % p-value2: % p \ r \ n", marry2 [0], marry2 [1]); NSLog (@ "array element address: value1: % p-value2: % p \ r \ n", marry3 [0], marry3 [1]); NSLog (@ "\ r \ n ------------------ after modifying the original value ---------------------- \ r \ n"); [mstr1 appendFormat: @ "aaa"]; NSLog (@ "marry1: % p-% @ \ r \ n ", marry1, marry1); NSLog (@" marry2: % p-% @ \ r \ n ", marry2, marry2 ); NSLog (@ "marry3: % p-% @ \ r \ n", marry3, marry3); NSLog (@ "array element address: value1: % p-value2: % p \ r \ n ", marry1 [0], marry1 [1]); NSLog (@" array element address: value1: % p-value2: % p \ r \ n ", marry2 [0], marry2 [1]); NSLog (@" array element address: value1: % p-value2: % p \ r \ n ", marry3 [0], marry3 [1]);}

Print result:

19:48:24. 539 beck. wang [1750: 230132] marry1: 0x60800004ae00-(value1, value2) 19:48:24. 539 beck. wang [1750: 230132] marry2: 0x608000023f00-(value1, value2) 19:48:24. 539 beck. wang [1750: 230132] marry3: 0x60800004abc0-(value1, value2) 19:48:24. 540 beck. wang [1750: 230132] array element address: value1: 0x60800006df40-value2: 0x60800006cb40 19:48:24. 540 beck. wang [1750: 230132] array element address: value1: 0x60800006df40-value2: 0x60800006cb40 19:48:24. 540 beck. wang [1750: 230132] array element address: value1: 0x60800006df40-value2: 0x60800006cb40 19:48:24. 540 beck. wang [1750: 230132] ------------------ after modifying the original value ------------------------ 19:48:24. 540 beck. wang [1750: 230132] marry1: 0x60800004ae00-(value1aaa, value2) 19:48:24. 540 beck. wang [1750: 230132] marry2: 0x608000023f00-(value1aaa, value2) 19:48:24. 540 beck. wang [1750: 230132] marry3: 0x60800004abc0-(value1aaa, value2) 19:48:24. 541 beck. wang [1750: 230132] array element address: value1: 0x60800006df40-value2: 0x60800006cb40 19:48:24. 541 beck. wang [1750: 230132] array element address: value1: 0x60800006df40-value2: 0x60800006cb40 19:48:24. 541 beck. wang [1750: 230132] array element address: value1: 0x60800006df40-value2: 0x60800006cb40

Analysis: before modifying the original value, the addresses of marry1, marry2, and marr3 are different. Obviously, copy and mutableCopy are both deep copies, but from the print results after the original value is modified, here, the deep copy is only a single-layer Deep copy: the memory address is newly opened, but the value in the array still points to the original array, so that after the original value is modified, the value in marry2 marr3 is modified. In addition, we can clearly see from the printed array element address that the addresses of the array elements of marry1, marry, and marr3 are identical before and after the modification, which further proves this point.

 

2.3 thought Extension: Full deep copy of collection objects

As mentioned in 2.2, deep copy is only a single-layer Deep copy for collection class objects. Is there any way to achieve deep copy for each layer? The answer is yes. Currently, we can do this:

(1) Archive Solution

-(Void) deplyFullCopy {NSMutableArray * marry1 = [NSMutableArray alloc] init]; NSMutableString * mstr1 = [[NSMutableString alloc] initWithString: @ "value1"]; NSMutableString * mstr2 = [[NSMutableString alloc] initWithString: @ "value2"]; [marry1 addObject: mstr1]; [marry1 addObject: mstr2: marry1]; NSArray * marray2 = [NSKeyedUnarchiver unarchiveTopLevelObjectWithData: data error: nil]; NSLog (@ "marry1: % p-% @ \ r \ n", marry1, marry1 ); NSLog (@ "marry2: % p-% @ \ r \ n", marray2, marray2); NSLog (@ "array element address: value1: % p-value2: % p \ r \ n ", marry1 [0], marry1 [1]); NSLog (@" array element address: value1: % p-value2: % p \ r \ n ", marray2 [0], marray2 [1]);}

Print result:

20:04:38. 726 beck. wang [1833: 242158] marry1: 0x600000048a00-(value1, value2) 20:04:38. 726 beck. wang [1833: 242158] marry2: 0x600000049780-(value1, value2) 20:04:38. 726 beck. wang [1833: 242158] array element address: value1: 0x600000066300-value2: 0x600000067000 20:04:38. 726 beck. wang [1833: 242158] array element address: value1: 0x600000066740-value2: 0x60020.66f40

Analysis: we can see that the new memory address is opened, and the pointer address of the array element is also different, achieving full deep copy.

(2)-(instancetype) initWithArray :( NSArray <ObjectType> *) array copyItems :( BOOL) flag;

-(Void) callback {NSMutableArray * marry1 = [[NSMutableArray alloc] init]; NSMutableString * mstr1 = [[NSMutableString alloc] initWithString: @ "value1"]; NSMutableString * mstr2 = [[NSMutableString alloc] initWithString: @ "value2"]; [marry1 addObject: mstr1]; [marry1 addObject: mstr2]; NSArray * marray2 = [[NSArray alloc] initWithArray: marry1 copyItems: YES]; NSLog (@ "marry1: % p-% @ \ r \ n", marry1, marry1 ); NSLog (@ "marry2: % p-% @ \ r \ n", marray2, marray2); NSLog (@ "array element address: value1: % p-value2: % p \ r \ n ", marry1 [0], marry1 [1]); NSLog (@" array element address: value1: % p-value2: % p \ r \ n ", marray2 [0], marray2 [1]);}

Print result:

20:08:04. 201 beck. wang [1868: 246161] marry1: 0x610000050320-(value1, value2) 20:08:04. 202 beck. wang [1868: 246161] marry2: 0x63792214c0-(value1, value2) 20:08:04. 202 beck. wang [1868: 246161] array element address: value1: 0x610000265600-value2: 0x610000266400 20:08:04. 202 beck. wang [1868: 246161] array element address: value1: 0xa001095756c61766-value2: 0xa003265756c61766

Analysis: Same as above.

 

Iii. Principles

No1: the copy and mutableCopy methods of a mutable object are both deep copies (the difference is full deep copy and single-layer Deep copy ).

No2: The copy method of immutable objects is light copy, and the mutableCopy method is deep copy.

No3: all objects returned by the copy method are immutable objects.

 

A map of thousands of words

 

 

After completing the in-depth copy, I will take some time to analyze the differences between memory definition statements. In view of the limited level, if there are any mistakes, I hope to criticize and correct them. Thank you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.