OC中的深复制与浅复制

来源:互联网 发布:乐高机器人编程教程 编辑:程序博客网 时间:2024/05/19 11:45

深浅拷贝的不同


浅拷贝是对对象发出一个retain操作,将指针赋值给新的集合对象,如图1

浅拷贝

NSArray *shallowCopyArray = [someArray copyWithZone:nil]; NSDictionary *shallowCopyDict = [[NSDictionary alloc] initWithDictionary:someDictionary copyItems:NO];

其中这些方法不局限于数组,也适用于array对象,其中copy默认调用的是copyWithZone,返回的是不可变的对象,mutalbeCopy默认调用mutalbeCopyWithZone,返回可变对象

深拷贝:

集合方法:initWithDicotonary copyItems:YES是进行深拷贝,它使每个集合内的元素接受copyWithZone消息,其中copyWithZone是浅拷贝,他只进行一层深拷贝

NSArray *deepCopyArray=[[NSArray alloc] initWithArray:someArray copyItems:YES];

如果需要进行纯深拷贝(多层次深拷贝),应该用如下方法,需要遵循NSCoding协议:

NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:          [NSKeyedArchiver archivedDataWithRootObject:oldArray]];

拷贝和可变性:

拷贝一个集合会对内部元素的可变性有影响,每个方法的影响都稍微不同,具体如下:

  • copyWithZone,返回不可变的首层对象(数组的话默认由消息的发送者进行retainCount+1新的array和旧的array,是首地址的拷贝),所有更深层次的可变性保持不变
  • initWithDictionary: copyItems:NO 返回可变的首层对象,所有更深层次的可变性保持不变
  • initWithDictionary: copyItems:YES返回可变的首层对象,下一层不可变(因为对每个内部对象发送了一次copyWithZone方法,如果为实现此方法将会报runtimeError),所有更深层次的可变性保持不变
  • 纯深拷贝所有可变性在任意层上保持不变
本文内容是对Apple官方文档CopyingCollection的翻译,括号内容是个人理解,如有不准确的地方请留言指出,谢谢
官方文档:https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Collections/Articles/Copying.html#//apple_ref/doc/uid/TP40010162-SW1
代码实验:
//copy 默认调用copyWithZone返回的是不可变的对象,mutableCopy 默认调用mutableCopyWithZone返回可变对象;二者都会对地址进行改变,查看NSObject API有解析    NSMutableString* im=[[NSMutableString alloc]initWithString:@"Inner_MM"];    NSString* isx=@"Inner_YYY";    //测试基本数据类型的copy 里面默认调用copyWithZone,产生新的副本    NSString* test=[im copy];    NSLog(@"im %ld",[im retainCount]);NSLog(@"test %ld",[test retainCount]);    NSLog(@"im %p",im);NSLog(@"test %p",test);    [im appendString:@"TEST"];//元数据改变新数据不变    NSLog(@"im %@",im);NSLog(@"test %@",test);    NSArray* a=[NSArray arrayWithObjects:im,isx, nil];//集合对象    //arr1的内容是 a(数组--里面有可变字符串im和不可变字符串isx) m(可变字符串) s(不可变字符串)    NSMutableString* m=[[NSMutableString alloc]initWithString:@"MM"];//retainCount1    NSString* s=@"YYY";    NSArray* arr1=[NSArray arrayWithObjects:a,m,s, nil];//m retainCount2加入数组加1;s(不可变字符串比较特殊)是不可变的永远都是-1    //retainCount是针对内存地址空间的计数器(arr1和arr2地址相同),与具体类别无关--个人理解    NSLog(@"arr1 %ld",[arr1 retainCount]);    NSLog(@"arr1 %p",arr1);    NSArray* arr2=[arr1 copyWithZone:nil];//copyWithZone返回的是消息的接受者    NSLog(@"copyWithZone a %ld",[a retainCount]);//2未改变    //arr1 和 arr2的retainCount均为2,所以是对arr的指针进行了retain(Copy的副本是隐士的被发送方retain--官方文档解释:NSCopying里面),他的释放应由发送方来进行释放    NSLog(@"copyWithZone arr1 %ld",[arr1 retainCount]);    NSLog(@"copyWithZone arr2 %ld",[arr2 retainCount]);    NSLog(@"copyWithZone arr2 %p",arr2);    //m的引用计数器和地址均相同,因此他们是同一个,所以此处是浅拷贝,但是m的retainCount是2    NSLog(@"copyWithZone arr1 m %ld",[[arr1 objectAtIndex:1]retainCount]);    NSLog(@"copyWithZone arr2 m %ld",[[arr2 objectAtIndex:1]retainCount]);    NSLog(@"copyWithZone arr1 m %p",[arr1 objectAtIndex:1]);    NSLog(@"copyWithZone arr2 m %p",[arr2 objectAtIndex:1]);    //initWithItem copyItems:NO 因为自己alloc地址肯定不一样,内容一样,因为进行了一次浅拷贝,内部数据进行retain+1    NSArray* arr3=[[NSArray alloc]initWithArray:arr1 copyItems:NO];    NSLog(@"initWithArray copyItems:NO a %ld",[a retainCount]);//3发送改变    NSLog(@"initWithArray copyItems:NO arr1 %ld",[arr1 retainCount]);    NSLog(@"initWithArray copyItems:NO arr3 %ld",[arr3 retainCount]);    NSLog(@"initWithArray copyItems:NO arr3 %p",arr3);    [im appendString:@"WW"];    [m appendString:@"3"];    NSLog(@"initWithArray copyItems:NO a %@",[arr1 objectAtIndex:0]);    NSLog(@"initWithArray copyItems:NO a %@",[arr3 objectAtIndex:0]);    NSLog(@"initWithArray copyItems:NO m %@",[arr1 objectAtIndex:1]);    NSLog(@"initWithArray copyItems:NO m %@",[arr3 objectAtIndex:1]);    NSLog(@"initWithArray copyItems:NO m %ld",[[arr1 objectAtIndex:1]retainCount]);//2+1=3    NSLog(@"initWithArray copyItems:NO m %ld",[[arr3 objectAtIndex:1]retainCount]);    //initWithItem copyItems:YES 因为自己alloc地址肯定不一样,如果是集合对象或自定义对象内容一样,普通对象不一样    NSArray* arr4=[[NSArray alloc]initWithArray:arr1 copyItems:YES];    NSLog(@"initWithArray copyItems:YES a %ld",[a retainCount]);//4    NSLog(@"initWithArray copyItems:YES arr1 %ld",[arr1 retainCount]);    NSLog(@"initWithArray copyItems:YES arr4 %ld",[arr4 retainCount]);    NSLog(@"initWithArray copyItems:YES arr4 %p",arr4);    [im appendString:@"FF"];    [m appendString:@"4"];    NSLog(@"initWithArray copyItems:YES a %@",[arr1 objectAtIndex:0]);    NSLog(@"initWithArray copyItems:YES a %@",[arr4 objectAtIndex:0]);    NSLog(@"initWithArray copyItems:YES m %@",[arr1 objectAtIndex:1]);    NSLog(@"initWithArray copyItems:YES m %@",[arr4 objectAtIndex:1]);    NSLog(@"initWithArray copyItems:YES m %ld",[[arr1 objectAtIndex:1]retainCount]);    NSLog(@"initWithArray copyItems:YES m %ld",[[arr4 objectAtIndex:1]retainCount]);//-1    NSLog(@"initWithArray copyItems:YES im %ld",[im retainCount]);//此时为2原因在于加入a的时候+1,此后一直没变,init不会对数组内数组元素发消息    //总结:1,copy里面默认调用copyWithZone,二者可以认为基本相同,如果自定义子类继承父类后有额外的变量,需要重写此方法,并且首先调用父类方法();2,copyWithZone是对数组指针进行retainCount+1;initWithArray copyItems:NO对数组内部数据进行+1(要区分是对数组+1还是里面的内容+1);3,initWithArray copyItems:YES,对数组内的每个元素接收一次copyWithZone方法(可查看 NSArray),因此,如果内部元素是数组类型还是浅复制即retainCount+1,而基本类型则是重新Copy一个新副本    //注:copy产生的是不可变的,所以arr4里面的index1输出的retainCount是-1;数组的操作和其他集合类似    



0 0
原创粉丝点击