什么是深、浅复制
浅复制:只是指针指向所复制的对象,共用一段内存
深复制:完全拷贝一份对象,不共用内存
iOS中深、浅复制总结
copy | mutableCopy | |
---|---|---|
不可变对象(NSString、NSArray) | 浅拷贝 | 深拷贝 |
可变对象(NSMutableString、NSMutableArray) | 深拷贝 | 深拷贝 |
NSString测试
NSString *str1 = [NSString stringWithFormat:@"abc"];
NSString *str2 = str1.copy;
NSString *str3 = str1.mutableCopy;
NSLog(@"str1 = %p",str1);
NSLog(@"str2 = %p",str2);
NSLog(@"str3 = %p",str3);
// 2016-06-30 15:41:32.314 测试深复制,浅复制[1475:450106] str1 = 0xa000000006362613
// 2016-06-30 15:41:32.315 测试深复制,浅复制[1475:450106] str2 = 0xa000000006362613
// 2016-06-30 15:41:32.315 测试深复制,浅复制[1475:450106] str3 = 0x144624f40
NSMutableString测试
NSMutableString *str1 = [NSMutableString stringWithFormat:@"abc"];
NSMutableString *str2 = str1.copy;
NSMutableString *str3 = str1.mutableCopy;
NSLog(@"str1 = %p-%@",str1,str1);
NSLog(@"str2 = %p-%@",str2,str2);
NSLog(@"str3 = %p-%@",str3,str3);
// 2016-06-30 15:47:16.537 测试深复制,浅复制[1489:452181] str1 = 0x12552e7a0-abc
// 2016-06-30 15:47:16.537 测试深复制,浅复制[1489:452181] str2 = 0xa000000006362613-abc
// 2016-06-30 15:47:16.537 测试深复制,浅复制[1489:452181] str3 = 0x1255340b0-abc
NSArray测试
NSArray *arr1 = [NSArray arrayWithObjects:@"a",@"b", nil];
NSArray *arr2 =arr1.copy;
NSArray *arr3 =arr1.mutableCopy;
NSLog(@"arr1=%p",arr1);
NSLog(@"arr2=%p",arr2);
NSLog(@"arr3=%p",arr3);
// 2016-06-30 15:49:32.252 测试深复制,浅复制[1493:452752] arr1=0x125547bd0
// 2016-06-30 15:49:32.253 测试深复制,浅复制[1493:452752] arr2=0x125547bd0
// 2016-06-30 15:49:32.253 测试深复制,浅复制[1493:452752] arr3=0x125541420
NSMutableArray测试
NSMutableArray *arr1 = [NSMutableArray arrayWithObjects:@"a",@"b", nil];
NSMutableArray *arr2 =arr1.copy;
NSMutableArray *arr3 =arr1.mutableCopy;
NSLog(@"arr1=%p",arr1);
NSLog(@"arr2=%p",arr2);
NSLog(@"arr3=%p",arr3);
// 2016-06-30 15:52:23.191 测试深复制,浅复制[1500:453692] arr1=0x147d22960
// 2016-06-30 15:52:23.191 测试深复制,浅复制[1500:453692] arr2=0x147d05ff0
// 2016-06-30 15:52:23.191 测试深复制,浅复制[1500:453692] arr3=0x147d10c40
NSDictionary测试
NSDictionary *dict1 = [NSDictionary new];
NSDictionary *dict2 = dict1.copy;
NSDictionary *dict3 = dict1.mutableCopy;
NSLog(@"dict1=%p",dict1);
NSLog(@"dict2=%p",dict2);
NSLog(@"dict3=%p",dict3);
// 2016-06-30 15:55:03.152 测试深复制,浅复制[1504:454390] dict1=0x135d003f0
// 2016-06-30 15:55:03.153 测试深复制,浅复制[1504:454390] dict2=0x135d003f0
// 2016-06-30 15:55:03.153 测试深复制,浅复制[1504:454390] dict3=0x135d32690
NSMutableDictionary测试
NSMutableDictionary *dict1 = [NSMutableDictionary new];
NSMutableDictionary *dict2 = dict1.copy;
NSMutableDictionary *dict3 = dict1.mutableCopy;
NSLog(@"dict1=%p",dict1);
NSLog(@"dict2=%p",dict2);
NSLog(@"dict3=%p",dict3);
// 2016-06-30 15:55:44.300 测试深复制,浅复制[1507:454706] dict1=0x13c51cda0
// 2016-06-30 15:55:44.300 测试深复制,浅复制[1507:454706] dict2=0x13c603c90
// 2016-06-30 15:55:44.300 测试深复制,浅复制[1507:454706] dict3=0x13c55d8b0
测试数组内部元素在拷贝过程中是否发生改变
NSMutableString *str1 = [NSMutableString stringWithFormat:@"str1"];
NSLog(@"str1 地址=%p",str1);
NSMutableString *str2 = [NSMutableString stringWithFormat:@"str2"];
NSLog(@"str2 地址=%p",str2);
NSMutableArray *mArr=[NSMutableArray arrayWithObjects:str1,str2, nil];
NSLog(@"mArr 地址=%p",mArr);
[[mArr objectAtIndex:0] appendString:@"append111"];
NSLog(@":append111:%@",str1);
NSLog(@"mArr[0] 地址=%p",[mArr objectAtIndex:0]);
NSArray *copyArr = mArr.copy;
[[mArr objectAtIndex:0] appendString:@"append222"];
NSLog(@"append222:%@",str1);
NSLog(@"copyArr 地址=%p",copyArr);
NSLog(@"copyArr[0] 地址=%p",[copyArr objectAtIndex:0]);
/*
测试容器类数据指针问题[2987:2525070] str1 地址=0x608000264a80
测试容器类数据指针问题[2987:2525070] str2 地址=0x60000026b280
测试容器类数据指针问题[2987:2525070] mArr 地址=0x608000053e60
测试容器类数据指针问题[2987:2525070] :append111:str1append111
测试容器类数据指针问题[2987:2525070] mArr[0] 地址=0x608000264a80
测试容器类数据指针问题[2987:2525070] append222:str1append111append222
测试容器类数据指针问题[2987:2525070] copyArr 地址=0x60800003fa80
测试容器类数据指针问题[2987:2525070] copyArr[0] 地址=0x608000264a80
*/
说明数组在copy过程中内容的指针还是指向了同一个地址