Deep replication: Not only copy objects but also objects associated with objects
Shallow copy: Simply copying the object itself does not copy objects associated with the object
////main.m//Replication of Objects////Created by MAC on 15/12/20.//copyright©2015 year MAC. All rights reserved.//#import<Foundation/Foundation.h>#import "Person.h"intMainintargcConst Char*argv[]) {@autoreleasepool {//deep Copy the object associated with the object is also copiednsmutablestring *m1 = [nsmutablestring stringwithstring:@" One"]; Nsmutablestring*m2 = [nsmutablestring stringwithstring:@" Both"]; Nsmutablestring*M3 = [nsmutablestring stringwithstring:@"three"]; Nsmutablearray*arr = [Nsmutablearray arraywithcapacity:Ten]; [Arr ADDOBJECT:M1]; [Arr addobject:m2]; [Arr ADDOBJECT:M3]; Nsmutablearray*ARR1 = [arr mutablecopy];//only objects associated with the assignment object itself do not copy shallow copyNSLog (@"%@", arr); NSLog (@"%@", arr1); //[arr removeobject:@ "one"];//Deleting a string does not affect[arr1 removeallobjects]; for(NSObject *sinchARR) {//Deep CopyNSObject *temp = [s mutablecopy];//take the object inside and copy it to another array .[arr1 addobject:temp]; } [M1 insertstring:@"changed"Atindex:1];//shallow copy the associated object that changes the array is also affected but the deep copy is unaffectedNSLog (@"%@", arr); NSLog (@"%@", arr1);// //Shallow Copy// //Copy// //mutablecopy Variablensmutablestring *str = [nsmutablestring stringwithstring:@"Hello World"]; Nsmutablestring*STR2 =str; NSLog (@"%p", str); NSLog (@"%p", STR2); [Str insertstring:@"Changed"Atindex:3]; //modification of the first second string is also modified because the memory address is the sameNSLog (@"%@", str); NSLog (@"%@", str2);//Nsmutablearray *array = [Nsmutablearray arraywithcapacity:Ten]; [Array AddObject:@"1"]; [Array AddObject:@"2"]; [Array AddObject:@"3"]; Nsmutablearray*array2 = array;//Same as memory addressNSLog (@"%p", array); NSLog (@"%p", array2); [Array Removeobject:@"2"]; Array2= [array mutablecopy];//Copy memory address is not the sameNSLog (@"%p", array); NSLog (@"%p", array2);//// //Custom Object ReplicationPerson *per =[[Person alloc]init]; Per->age = -; person*P2 = per;//memory address as NSStringPerson *P3 = [per copy];//-[person Copywithzone:]: Unrecognized selector sent to instance did not find this method need to implement the Nscopying protocol inside-(ID) Copywithzone: ( Nullable Nszone *) Zone; methodNSLog (@"=%p", per); NSLog (@"=%p", p3); NSLog (@"%d",p2->Age ); Per->age =Ten; NSLog (@"%d",p2->Age ); Nsmutableset*mset = [Nsmutableset setwithcapacity:Ten]; [Mset AddObject:@" One"]; [Mset AddObject:@" Both"]; [Mset AddObject:@"three"]; [Mset AddObject:@" Four"]; Nsmutableset*mset2 = [Mset mutablecopy];//using mutablecopy memory address is not the sameNSLog (@"%p", Mset); NSLog (@"%p", Mset2); } return 0;}
////Person.h//Replication of Objects////Created by MAC on 15/12/20.//copyright©2015 year MAC. All rights reserved.//#import<Foundation/Foundation.h>@interfacePerson:nsobject<nscopying>{ @public intAge ; } @property (nonatomic)intAge ; @property (nonatomic,copy) NSString*Name: @property (nonatomic)intnum;@end////person.m//Replication of Objects////Created by MAC on 15/12/20.//copyright©2015 year MAC. All rights reserved.//#import "Person.h"@implementation Person- (ID) Copywithzone: (Nullable Nszone *) zone{ person*per =[[Person allocwithzone:zone]init]; NSLog (@"copy per"); returnper;}@end
Deep copy Shallow copy