浅拷贝(Shallow Copy)与深拷贝(Deep Copy)
在Objective-C中并不是所有的对象都支持Copy,MutableCopy,遵守NSCopying协议的类才可以发送Copy消息,遵守NSMutableCopying 协议的类才可以发送MutableCopy消息.
浅拷贝,并不拷贝对象本身,仅仅是拷贝指向对象的指针;
深拷贝是直接拷贝整个对象内存到另一块内存中
浅拷贝就是指针拷贝;深拷贝就是内容拷贝。
非集合类对象的copy与mutableCopy
指的是 NSString, NSNumber ... 之类的对象
/*
系统非集合类对象指的是 NSString, NSNumber ... 之类的对象
*/
NSString *string1 = @"shallow copy/one-level-deep copy";
NSString *string2 = [string1 copy];
NSString *string3 = [string1 mutableCopy];
NSLog(@"string1:%p", string1);
NSLog(@"string2:%p", string2);
NSLog(@"string3:%p", string3);
NSLog(@"---------------------------");
NSMutableString *string4 = [NSMutableString stringWithString:@"shallow copy/one-level-deep copy"];
NSMutableString *string5 = [string4 copy];
NSMutableString *string6 = [string4 mutableCopy];
NSString *string7 = [string4 copy];
NSString *string8 = [string4 mutableCopy];
NSLog(@"string4:%p", string4);
NSLog(@"string5:%p", string5);
NSLog(@"string6:%p", string6);
NSLog(@"string7:%p", string7);
NSLog(@"string8:%p", string8);
// [string4 appendString:@"123"];//不注释,会在此行crash,原因就是 copy 返回的对象是 immutable 对象
// [string5 appendString:@"234"];
// [string6 appendString:@"345"];
运行以上代码打印结果:
2016-07-26 20:05:07.805 深拷贝和浅拷贝[3213:182622] string1:0x10c6dd0b8
2016-07-26 20:05:07.805 深拷贝和浅拷贝[3213:182622] string2:0x10c6dd0b8
2016-07-26 20:05:07.805 深拷贝和浅拷贝[3213:182622] string3:0x7f85ab410c70
2016-07-26 20:05:07.806 深拷贝和浅拷贝[3213:182622] ---------------------------
2016-07-26 20:05:07.806 深拷贝和浅拷贝[3213:182622] string4:0x7f85ab412ad0
2016-07-26 20:05:07.806 深拷贝和浅拷贝[3213:182622] string5:0x7f85ab410f10
2016-07-26 20:05:07.806 深拷贝和浅拷贝[3213:182622] string6:0x7f85ab439fa0
2016-07-26 20:05:07.806 深拷贝和浅拷贝[3213:182622] string7:0x7f85ab424590
2016-07-26 20:05:07.806 深拷贝和浅拷贝[3213:182622] string8:0x7f85ab4245d0
得出结论
在非集合类对象中:对immutable对象进行copy操作,是指针拷贝,mutableCopy操作时内容拷贝;对mutable对象进行copy和mutableCopy都是内容拷贝
。用代码简单表示如下:
[immutableObject copy] // 浅拷贝
[immutableObject mutableCopy] //深拷贝
[mutableObject copy] //深拷贝
[mutableObject mutableCopy] //深拷贝
集合类对象的copy与mutableCopy
集合类对象是指NSArray、NSDictionary、NSSet ... 之类的对象
NSArray *array1 = @[@"123",@"234"];
NSArray *array2 = [array1 copy];
NSArray *array3 = [array1 mutableCopy];
NSLog(@"array1:%p", array1);
NSLog(@"array2:%p", array2);
NSLog(@"array3:%p", array3);
NSLog(@"---------------------------");
NSLog(@"array1[0]:%p", array1[0]);
NSLog(@"array2[0]:%p", array2[0]);
NSLog(@"array3[0]:%p", array3[0]);
NSLog(@"---------------------------");
NSLog(@"---------------------------");
NSMutableArray *array4 = [NSMutableArray array];
NSString *string1 = @"123";
NSString *string2 = @"234";
[array4 addObject:string1];
[array4 addObject:string2];
NSMutableArray *array5 = [array1 copy];
NSMutableArray *array6 = [array1 mutableCopy];
NSLog(@"array4:%p", array4);
NSLog(@"array5:%p", array5);
NSLog(@"array6:%p", array6);
NSLog(@"---------------------------");
NSLog(@"array4[0]:%p", array4[0]);
NSLog(@"array5[0]:%p", array5[0]);
NSLog(@"array6[0]:%p", array6[0]);
}
运行以上代码打印结果:
2016-07-26 19:47:17.549 深拷贝和浅拷贝[3122:168450] array1:0x7fef5b61f930
2016-07-26 19:47:17.550 深拷贝和浅拷贝[3122:168450] array2:0x7fef5b61f930
2016-07-26 19:47:17.550 深拷贝和浅拷贝[3122:168450] array3:0x7fef5b6163b0
2016-07-26 19:47:17.550 深拷贝和浅拷贝[3122:168450] ---------------------------
2016-07-26 19:47:17.550 深拷贝和浅拷贝[3122:168450] array1[0]:0x100a880b8
2016-07-26 19:47:17.550 深拷贝和浅拷贝[3122:168450] array2[0]:0x100a880b8
2016-07-26 19:47:17.551 深拷贝和浅拷贝[3122:168450] array3[0]:0x100a880b8
2016-07-26 19:47:17.551 深拷贝和浅拷贝[3122:168450] ---------------------------
2016-07-26 19:47:17.551 深拷贝和浅拷贝[3122:168450] array4:0x7fef5b60db80
2016-07-26 19:47:17.551 深拷贝和浅拷贝[3122:168450] array5:0x7fef5b61f930
2016-07-26 19:47:17.551 深拷贝和浅拷贝[3122:168450] array6:0x7fef5b614600
2016-07-26 19:47:17.552 深拷贝和浅拷贝[3122:168450] ---------------------------
2016-07-26 19:47:17.552 深拷贝和浅拷贝[3122:168450] array4[0]:0x100a880b8
2016-07-26 19:47:17.552 深拷贝和浅拷贝[3122:168450] array5[0]:0x100a880b8
2016-07-26 19:47:17.579 深拷贝和浅拷贝[3122:168450] array6[0]:0x100a880b8
得出结论
在集合类对象中,对immutable对象进行copy,是指针拷贝,mutableCopy是内容拷贝;对mutable对象进行copy和mutableCopy都是内容拷贝。
集合对象的内容拷贝仅限于对象本身,对象元素仍然是指针拷贝。(此处的内容拷贝,仅仅是拷贝array这个对象,array集合内部的元素仍然是指针拷贝)
用代码简单表示如下:
[immutableObject copy] // 浅拷贝
[immutableObject mutableCopy] //单层深拷贝
[mutableObject copy] //单层深拷贝
[mutableObject mutableCopy] //单层深拷贝