使用copy或mutableCopy方法可以创建一个对象的副本
copy
需要实现NSCoppying协议
这些创建的是不可变副本(如NSString、NSArray、NSDictionary)
mutableCopy
需要先实现NSMutableCopying协议
创建的是可变副本(如NSMutableString、NSMutableArray、NSMutableDictionary)
Copy的目的是建立副本,同时修改原始对象和复本不会互相干扰
内容拷贝,源对象和副本指向的是不同的两个对象
源对象引用计数器不变,副本计数器设置为1
指针拷贝,源对象和副本指向的是同一个对象
对象的引用计数器+1,其实相当于做了一次retain操作
只有不可变对象创建不可变副本(copy)才是浅复制,其他都是深复制
为什么要有copy与mutableCopy,系统尽可能的节省内存开销
// 测试不可变对象的copy
NSString *string = @"瞅啥尼aaa";
NSString *copyString = [string copy];
NSLog(@"string ===%p copyString = %p",string,copyString);
// 运行结果test_Copy[21354:420763] string ===0x108728068 copyString = 0x108728068
// 以上说明copy对不可变对象是浅拷贝
NSLog(@"=============\n");
// 测试不可变对象的mutableCopy
NSString *mutableCopyString = [string mutableCopy];
NSLog(@"string ===%p mutableCopyString = %p",string,mutableCopyString);
// 运行结果:test_Copy[21354:420763] string ===0x108728068 mutableCopyString = 0x60400005fc20
// 以上说明mutableCopy对不可变对象是深拷贝
NSLog(@"=============\n");
// 测试可变对象的copy
NSMutableString *mutableString = [[NSMutableString alloc] initWithString:@"瞅啥尼bbb"];
NSString *copyMutableString = [mutableString copy];
NSLog(@"mutableString ===%p mutableStringCopy = %p",mutableString,copyMutableString);
// 运行结果: test_Copy[21354:420763] mutableString ===0x60000005e360 mutableStringCopy = 0xa000000006262623
// 以上说明copy对可变对象是深拷贝
NSLog(@"=============\n");
// 测试可变对象的mutableCopy
NSString *mutableCopymutableString = [mutableString mutableCopy];
NSLog(@"mutableString ===%p mutableStringMutableCopy = %p",mutableString,mutableCopymutableString);
// 运行结果 test_Copy[21524:427609] mutableString ===0x60400004ed30 mutableStringMutableCopy = 0x60c000055900
// 以上说明mutableCopy对可变对象是深拷贝
Person *person1 = [Person new];
person1.name = @"一号";
Person *person2 = [Person new];
person2.name = @"二号";
// 测试不可变容器对象的copy
NSArray *array = @[person1,person2];
NSArray *copyArray = [array copy];
NSLog(@"array ==%p array ===%@ \n copyArray = %p copyArray = %@",array,array,copyArray,copyArray);
// 运行结果:
/* array ==0x6000000393c0 array ===(
"",
""
)
copyArray = 0x6000000393c0 copyArray = (
"",
""
) */
// copy 对不可变容器对象作用后,对象指针地址没有变化,容器内元素地址没变
// 以上说明copy对不可变容器对象是浅拷贝,
NSLog(@"=============\n");
// 测试不可变容器对象的mutableCopy
NSArray *mutableCopyArray = [array mutableCopy];
NSLog(@"array ==%p array ===%@ \n mutableCopyArray = %p mutableCopyArray = %@",array,array,mutableCopyArray,mutableCopyArray);
// 运行结果:
/*
test_Copy[23761:495549] array ==0x60400003df00 array ===(
"",
""
)
mutableCopyArray = 0x604000053080 mutableCopyArray = (
"",
""
) */
// mutableCopy 对不可变容器作用后,开辟了一块心得内存,容器内元素地址没变
NSLog(@"=============\n");
// 测试可变容器对象的copy
NSMutableArray *mutableArray = [NSMutableArray arrayWithArray:array];
NSArray *copyMutableArray = [mutableArray copy];
NSLog(@"mutableArray ==%p mutableArray ===%@ \n copyMutableArray = %p copyMutableArray = %@",mutableArray,mutableArray,copyMutableArray,copyMutableArray);
// 运行结果:
/*test_Copy[23970:503539] mutableArray ==0x600000440db0 mutableArray ===(
"",
""
)
copyMutableArray = 0x60000003db00 copyMutableArray = (
"",
""
) */
// copy 对可变容器作用后,开辟了一块新得内存,容器内元素地址没变
// 测试可变容器对象的mutableCopy
NSArray *mutableCopyMutableArray = [mutableArray mutableCopy];
NSLog(@"mutableArray ==%p mutableArray ===%@ \n mutableCopyMutableArray = %p mutableCopyMutableArray = %@",mutableArray,mutableArray,mutableCopyMutableArray,mutableCopyMutableArray);
// 运行结果:
/*test_Copy[24128:510612] mutableArray ==0x6000002409c0 mutableArray ===(
"",
""
)
mutableCopyMutableArray = 0x608000248d00 mutableCopyMutableArray = (
"",
""
)*/
// mutableCopy对可变容器作用后,开辟了一块新得内存,容器内元素地址没变
对于不可变对象,copy是浅拷贝,mutableCopy是深拷贝。
对于可变对象,无论是copy还是mutableCopy都是深拷贝
如果是容器类的对象,比如NSArray,NSMutableArray,无论是深拷贝还是钱拷贝,容器内元素的指针总是拷贝之前的指针,也就是元素只进行了浅拷贝