1. 先说结论:
分可变对象、不可变对象、自定义对象、容器对象几种:
对象类型 | copy | mutableCopy |
---|---|---|
不可变对象 | 浅拷贝 | 深拷贝 |
可变对象 | 深拷贝 | 深拷贝 |
自定义对象 | 深拷贝 | 深拷贝 |
容器对象 | 浅拷贝 | 深拷贝 |
例子参考: iOS开发——深拷贝与浅拷贝详解
注解:
- 所有mutableCopy都是深拷贝,所以只需要记住不可变对象和容器对象(其实就是不可变对象)是浅拷贝就行了
- 另外要注意得到对象的类型,有一种最特殊,就是可变对象copy之后得到的是个不可变对象,所以一个NSMutableString类型的property,一般应该用strong关键字
- 容器对象的深拷贝只是对象本身深拷贝,其元素依然是浅拷贝,如NSMuatableArray *array1,对其调用copy或者mutableCopy,都是生成一个新的对象,但里边元素的地址是不变的,如下边示例2
示例1:可变对象copy的例子
NSMutableString *mutString = [NSMutableString stringWithString:@"abc"];
NSMutableString *copyString = [mutString copy];
NSMutableString *mutableCopyString = [mutString mutableCopy];
NSLog(@"mutString:%p,copyString:%p,mutableCopyString:%p",mutString,copyString,mutableCopyString);
[copyString appendString:@"aaaa"]; //此处会引发崩溃
2018-03-21 15:03:55.706167+0800 iOSLearnigDemo[89468:8764272] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa000000006362613'
示例2:容器对象copy的例子
// 容器对象
NSArray *array = [NSArray arrayWithObject:@(1)];
NSArray *copyArray = [array copy];
NSMutableArray *mutableCopyArray = [array mutableCopy];
NSLog(@"array:%p,copyArray:%p,mutableCopyArray:%p",array,copyArray,mutableCopyArray);
// 可变容器对象
NSMutableArray *mutableArray = [NSMutableArray arrayWithObject:@(2)];
NSArray *copyMutableArray = [mutableArray copy];
NSArray *mutableCopyMutableArray = [mutableArray mutableCopy];
NSLog(@"mutableArray:%p,copyMutableArray:%p,mutableCopyMutableArray:%p",mutableArray,copyMutableArray,mutableCopyMutableArray);
// 打印新array中元素的地址,由下图可见元素地址都是相同的,所以深拷贝只拷贝对象本身,其元素依然是浅拷贝
NSLog(@"mutableArray obj:%p,copyMutableArray obj:%p,mutableCopyMutableArray obj:%p",mutableArray.firstObject,copyMutableArray.firstObject,mutableCopyMutableArray.firstObject);
2018-03-21 15:07:27.414277+0800 iOSLearnigDemo[89534:8778488] array:0x600000015870,copyArray:0x600000015870,mutableCopyArray:0x60000024b340
2018-03-21 15:07:27.415237+0800 iOSLearnigDemo[89534:8778488] mutableArray:0x60000024ee80,copyMutableArray:0x6000000158e0,mutableCopyMutableArray:0x600000252cf0
2018-03-21 15:07:27.416033+0800 iOSLearnigDemo[89534:8778488] mutableArray obj:0xb000000000000022,copyMutableArray obj:0xb000000000000022,mutableCopyMutableArray obj:0xb000000000000022
2. 自定义对象
要支持copy与mutableCopy,需要实现NSCopying,NSMutableCopying协议,用法如下:
(Foundation框架对象都实现了该协议,所以可以直接使用copy与mutableCopy,UIKit框架不可以)
@interface Person()<NSCopying,NSMutableCopying>
@end
@implementation Person
- (id)copyWithZone:(NSZone *)zone{
Person *person = [Person allocWithZone:zone];
person.name = self.name;
person.year = self.year;
return person;
}
- (id)mutableCopyWithZone:(NSZone *)zone{
Person *person = [Person allocWithZone:zone];
person.name = self.name;
person.year = self.year;
return person;
}
@end
// 简单的调用代码
Person *person = [[Person alloc] init];
person.name = @"luofuzhong";
person.year = 26;
Person *copyPerson = [person copy];
Person *mutableCopyPerson = [person mutableCopy];
NSLog(@"person:%p,copyPerson:%p,mutableCopyPerson:%p",person,copyPerson,mutableCopyPerson);
输出结果如下:
2018-03-21 14:42:04.214203+0800 iOSLearnigDemo[89280:8704219] person:0x60000023df20,copyPerson:0x60000023edc0,mutableCopyPerson:0x60000023f820