Deep copy and shallow copy

Source: Internet
Author: User

Deep copy and shallow copy requires compliance with Nscopying, nsmutablecopying 2 protocols

What is the literal meaning of copycopy is "copy", "Copy", is a copy of the process common replication is: File copy function: The use of a source file to produce a copy of the file features: Modify the contents of the source file, will not affect the copy file to modify the contents of the copy file, Does not affect the copy role in the source file OC: Using a source object to produce a Copy object feature: Modifying the properties and behavior of the source object does not affect the Copy object modifying the properties and behavior of the replica object, without affecting the source object

Deep copy (deep copy, content copy, depth copy) source object and copy object are different two object source object reference counter unchanged, copy object counter is 1 (because it is newly produced) essence is: New object is generated shallow copy (shallow copy, pointer copy, shallow copy) The source object and the replica object are the same object source object (Replica object) reference counter + 1, which is equivalent to doing a retain operation essentially: no new objects are produced

Example 1 NSString and nsmutablestring:

NSString *STR1 = @ "abc";    NSString *STR2 = @ "abc";        BOOL isequals = str1==str2? Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");        NSString *STR3 = [str1 copy];    Isequals = str3==str2? Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");        nsmutablestring *STR4 = [str1 mutablecopy];    Isequals = str4==str1? Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");        NSLog (@ "-----------------------");        NSString *STR5 = @ "abc";    nsmutablestring *STR6 = [nsmutablestring stringwithformat:@ "abc"];    Isequals = STR5==STR6? Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");        NSString *STR7 = [STR6 copy];    Isequals = str7==str2? Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");        nsmutablestring *str8 = [STR6 mutablecopy];    Isequals = STR8==STR6? Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");

Operation Result:

2015-01-01 14:48:07.742 deep copy with shallow copy [934:56,160] = same =2015-01-01 14:48:07.743 deep copy with shallow copy [934:56,160] = same =2015-01-01 14:48:07.743 Deep copy with shallow copy [934:56,160] = not identical =2015-01-01 14:48:07.743 deep copy with shallow copy [934:56160]-----------------------2015-01-01 14:48:07.744 Deep copy with shallow copy [934:56,160] = not identical =2015-01-01 14:48:07.744 deep copy with shallow copy [934:56,160] = not identical =2015-01-01 14:48:07.744 deep copy with shallow copy [934:56,160] = Not the same =program ended with exit code:0

Note: nsstring copy produces immutable data objects that also point to the original object, above str1 and STR3

Example 2 Nsarray and Nsmutablearray:

 Nsarray *array1 = @[@1,@2,@3];        Nsarray *array2 = @[@1,@2,@3]; BOOL Isequals = Array1 = = array2?    Yes:no;        NSLog (@ "=%@=", [email protected] "Same": @ "not the same");    Nsarray *array3 = [array1 copy]; Isequals = Array3 = = array1?    Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");    Nsmutablearray *array4 = [Array1 mutablecopy]; Isequals = Array4 = = array1?    Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");//NSLog (@ "%@", array1);//NSLog (@ "%@", Array4);    [Array4 addobject:@4];//NSLog (@ "%@", array1);        NSLog (@ "%@", Array4);        NSLog (@ "--------------------------");      Nsmutablearray *array5 = [Nsmutablearray arraywithobjects:@1,@2,@3, Nil];    Nsarray *array6 = [array5 copy]; Isequals = Array5 = = Array6?    Yes:no;        NSLog (@ "=%@=", [email protected] "Same": @ "not the same");    Nsmutablearray *array8 = [Array5 mutablecopy]; Isequals = Array5 = = Array8?    Yes:no; NSLog (@ "=%@=", [email protected] "Same": @ "not the same");//NSLog (@ "%@", ARRay8);    [Array8 addobject:@4];//NSLog (@ "%@", array5); NSLog (@ "%@", Array8);

Operation Result:

2015-01-01 15:00:30.950 deep copy with shallow copy [980:59,234] = not identical =2015-01-01 15:00:30.951 deep copy with shallow copy [980:59,234] = same =2015-01-01 15:00:30.951 deep copy with shallow copy [980:59,234] = not identical =2015-01-01 15:00:30.952 deep copy with shallow copy [980:59234] (    1,    2,    3,    4) 2015-01-01 15:00:30.952 deep copy with shallow copy [980:59234]--------------------------2015-01-01 15:00:30.952 deep copy with shallow copy [980:59,234] = not identical =2015-01-01 15:00:30.952 deep copy with shallow copy [980:59,234] = not identical =2015-01-01 15:00:30.952 deep copy with shallow copy [980:59234] (    1,    2,    3,    4) Program ended with exit code:0

Note: Nsarray copy produces immutable data objects that also point to the original object, above Array1 and Array3

Example 4 Nsdictionary and nsmutabledictionary are the same as above:

Example 5: Test whether a variable or immutable object is created, the array stores the content object or points to the same object

@interface person:nsobject//<nscopying, nsmutablecopying> @property (nonatomic,copy) nsstring *name; @property ( nonatomic,assign) int age;-(void) eat;-(instancetype) Initwithname: (NSString *) name Age: (int) age;+ (instancetype) Personwithname: (NSString *) name Age: (int.) age; @end

#import "Person.h" @implementation person-(void) eat{    NSLog (@ "%s", __func__);} + (Instancetype) Personwithname: (NSString *) name Age: (int.) age{    return [[Self alloc] initwithname:name age:age];} -(Instancetype) Initwithname: (NSString *) name Age: (int.) age{    if (self = = [Super init]) {        self.name = name;        Self.age = age;    }    return self;} -(NSString *) description{    return [nsstring stringwithformat:@ "name=%@ age=%d", _name,_age];} -(ID) Copywithzone: (Nszone *) zone{//person    *person = [person Allocwithzone:zone] init];//    person.name = [ Self.name copy];//    person.age = self.age;//    return person;//}////-(ID) Mutablecopywithzone: (Nszone *) zone{ Person    *person = [[Person Allocwithzone:zone] init];//    person.name = [Self.name mutablecopy];//    Person.age = self.age;//    return person;//} @end

Person *P1 = [Person personwithname:@ "Zhangsan" age:20];    Person *P2 = [Person personwithname:@ "Lisi" age:21];    Person *P3 = [Person personwithname:@ "Wangwu" age:22];        Nsmutablearray *array1 = [Nsmutablearray arraywithobjects:p1,p2,p3, nil];    NSLog (@ "%@", array1);    P1.name = @ "zhangsan111";    NSLog (@ "%@", array1);        Nsarray *array2 = [array1 copy];    NSLog (@ "%@", array2);        BOOL Isequals = Array1 = = array2? Yes:no;    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");        Isequals = array1[0] = = array2[0];    NSLog (@ "=%@=", [email protected] "Same": @ "not the same");

Operation Result:

2015-01-01 15:18:58.814 deep copy with shallow copy [1100:64607] (    "Name=zhangsan age=20",    "Name=lisi age=21",    "Name=wangwu Age=22 ") 2015-01-01 15:18:58.815 deep copy with shallow copy [1100:64607] (    " name=zhangsan111 age=20 ",    " Name=lisi age=21 ",    " Name=wangwu age=22 ") 2015-01-01 15:18:58.815 deep copy with shallow copy [1100:64607] (    " name=zhangsan111 age=20 ",    " Name=lisi age= ",    " Name=wangwu age=22 ") 2015-01-01 15:18:58.816 deep copy with shallow copy [1100:64,607] = not identical =2015-01-01 15:18:58.816 deep copy with shallow copy [ 1100:64,607] = same =

indicates that the content objects stored in the 2 array after replication are the same .

Deep copy and shallow copy

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.