IOS depth Copy

Source: Internet
Author: User

-(void) copydemo{//In a non-collection class object: Copy the Immutable object, copy the pointer, copy the contents of the mutablecopy operation, and copy and mutablecopy the Mutable object as content replication. The code is simply represented as follows://    //[immutableobject Copy]//Shallow Copy//[Immutableobject mutablecopy]//Deep Copy//[mutableobject Copy]//Deep Copy//[Mutableobject mutablecopy]//Deep Copy//In the collection class object, copy the immutable object, which is the pointer copy, the mutablecopy is the content copy, and the copy and Mutablecopy of the Mutable object are content replication. However: the content copy of the collection object is limited to the object itself, and the object element is still a pointer copy. The code is simply represented as follows://    //[immutableobject Copy]//Shallow Copy//[Immutableobject mutablecopy]//single layer deep copy//[mutableobject Copy]//single layer deep copy//[Mutableobject mutablecopy]//single layer deep copy//Baby *baby = [[Baby alloc] init];//The copy method must be implemented//baby.babyname = @ "Wei";nsmutabledictionary*dic = [[Nsmutabledictionary alloc] Initwithcapacity:Ten]; [DiC setobject:@"1"Forkey:@"Key1"]; Nsmutablearray*array1 =[[Nsmutablearray alloc] initwithobjects:dic,dic, nil]; Nsmutablearray*array2 = [Array1 mutablecopy];//single layer deep copyNsmutablearray *array3 = [[Nsmutablearray alloc] Initwitharray:array1 Copyitems:yes];//the dic becomes immutable.nsmutablearray* Array4 =[Nskeyedunarchiver unarchiveobjectwithdata: [Nskeyedarchiver archiveddatawithrootobj Ect:array1]];//inside the dic change does not affect other arrays (which and methods affect other arrays)[Array2 Removeobjectatindex:0]; [[Array4 Objectatindex:0] SetObject:@"3"Forkey:@"Key1"];//[[Array4 objectatindex:1] setbabyname:@ "8888"];DLog (@"%@ \%@ \%@ \%@", Array1,array2,array3,array4);}

IOS depth 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.