我们从3种类型的对象来分析一下OC的深浅拷贝
系统非容器类型的对象(以string来举例)
- 不可变类型 NSString
NSString *originString = @"123";
id copyString = [originString copy];
id mCopyString = [originString mutableCopy];
NSLog(@"originString:%p", originString);
NSLog(@"copyString:%p", copyString);
NSLog(@"mCopyString:%p", mCopyString);
2020-01-19 14:03:17.937813+0800 Demo[36969:6480975] originString:0x10ed822a8
2020-01-19 14:03:17.937928+0800 Demo[36969:6480975] copyString:0x10ed822a8
2020-01-19 14:03:17.937997+0800 Demo[36969:6480975] mCopyString:0x600000a1c1e0
可以根据打印的指针指向的内存地址发现,此种情况:copy为浅拷贝、mutaleCopy为深拷贝
- 可变类型 NSMutableString
NSMutableString *originString = [[NSMutableString alloc] initWithString:@"123"];
id copyString = [originString copy];
id mCopyString = [originString mutableCopy];
NSLog(@"originString:%p", originString);
NSLog(@"copyString:%p", copyString);
NSLog(@"mCopyString:%p", mCopyString);
2020-01-19 14:12:13.527018+0800 Demo[36997:6505270] originString:0x600002902280
2020-01-19 14:12:13.527129+0800 Demo[36997:6505270] copyString:0xec76ab5ba268bd7b
2020-01-19 14:12:13.527193+0800 Demo[36997:6505270] mCopyString:0x6000029022b0
可以根据打印的指针指向的内存地址发现,此种情况:copy为深拷贝、mutaleCopy为深拷贝
系统容器类型对象 (以array来举例)
- 不可变容器类型 NSArray
NSArray *originArray = @[@"1"];
id copyArray = [originArray copy];
id mCopyArray = [originArray mutableCopy];
NSLog(@"originArray:%p", originArray);
NSLog(@"copyArray:%p", copyArray);
NSLog(@"mCopyArray:%p", mCopyArray);
NSLog(@"originArrayObject:%p", [originArray firstObject]);
NSLog(@"copyArrayObject:%p", [copyArray firstObject]);
NSLog(@"mCopyArrayObject:%p", [mCopyArray firstObject]);
2020-01-19 14:40:21.960024+0800 Demo[37172:6543895] originArray:0x600001ee01f0
2020-01-19 14:40:21.960119+0800 Demo[37172:6543895] copyArray:0x600001ee01f0
2020-01-19 14:40:21.960263+0800 Demo[37172:6543895] mCopyArray:0x6000012b0090
2020-01-19 14:40:21.960386+0800 Demo[37172:6543895] originArrayObject:0x107f882a8
2020-01-19 14:40:21.960457+0800 Demo[37172:6543895] copyArrayObject:0x107f882a8
2020-01-19 14:40:21.960508+0800 Demo[37172:6543895] mCopyArrayObject:0x107f882a8
可以根据打印结果发现 copy的时候,数组的地址没有发生变化,数组内部数的地址也未发生变化。mutableCopy的时候,数组的地址发生了变化,但是内部数据的地址未发生变化。所以copy和mutableCopy都是浅拷贝。
那么这种情况如何来进行深拷贝呢?
归档解档
NSArray *originArray = @[@"1"];
// 归档
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.data"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:originArray requiringSecureCoding:NO error:nil];
if (data) {
[data writeToFile:filePath atomically:YES];
}
// 解档
NSData *unArcData = [NSData dataWithContentsOfFile:filePath];
NSArray *unArcArray = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:unArcData error:nil];
NSLog(@"originArray:%p", originArray);
NSLog(@"unArcArray:%p", unArcArray);
NSLog(@"originArrayObject:%p", [originArray firstObject]);
NSLog(@"unArcArrayObject:%p", [unArcArray firstObject]);
2020-01-19 15:09:01.184102+0800 Demo[37268:6618589] originArray:0x600001e3c360
2020-01-19 15:09:01.184202+0800 Demo[37268:6618589] unArcArray:0x600001e3c370
2020-01-19 15:09:01.184265+0800 Demo[37268:6618589] originArrayObject:0x1039dd2b0
2020-01-19 15:09:01.184332+0800 Demo[37268:6618589] unArcArrayObject:0x8450bf31daaf8e9f
根据打印结果发现,归档解档实现了此类型的深拷贝
- 可变类型容器 NSMutableArray
NSMutableArray *originArray = [[NSMutableArray alloc] initWithObjects:@"1", nil];
id copyArray = [originArray copy];
id mCopyArray = [originArray mutableCopy];
NSLog(@"originArray:%p", originArray);
NSLog(@"copyArray:%p", copyArray);
NSLog(@"mCopyArray:%p", mCopyArray);
NSLog(@"originArrayObject:%p", [originArray firstObject]);
NSLog(@"copyArrayObject:%p", [copyArray firstObject]);
NSLog(@"mCopyArrayObject:%p", [mCopyArray firstObject]);
2020-01-19 15:31:27.721088+0800 Demo[37395:6678346] originArray:0x600001c5c5a0
2020-01-19 15:31:27.721183+0800 Demo[37395:6678346] copyArray:0x60000102c380
2020-01-19 15:31:27.721233+0800 Demo[37395:6678346] mCopyArray:0x600001c5c7b0
2020-01-19 15:31:27.721278+0800 Demo[37395:6678346] originArrayObject:0x10898d2a8
2020-01-19 15:31:27.721336+0800 Demo[37395:6678346] copyArrayObject:0x10898d2a8
2020-01-19 15:31:27.721393+0800 Demo[37395:6678346] mCopyArrayObject:0x10898d2a8
可以根据打印结果发现 copy的时候,数组的地址发生变化,数组内部数的地址未发生变化。mutableCopy的时候,数组的地址发生了变化,内部数据的地址未发生变化。所以copy和mutableCopy都是浅拷贝。
那么这种情况如何来进行深拷贝呢?
归档解档
NSMutableArray *originArray = [[NSMutableArray alloc] initWithObjects:@"1", nil];
// 归档
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"array.data"];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:originArray requiringSecureCoding:NO error:nil];
if (data) {
[data writeToFile:filePath atomically:YES];
}
// 解档
NSData *unArcData = [NSData dataWithContentsOfFile:filePath];
NSArray *unArcArray = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:unArcData error:nil];
NSLog(@"originArray:%p", originArray);
NSLog(@"unArcArray:%p", unArcArray);
NSLog(@"originArrayObject:%p", [originArray firstObject]);
NSLog(@"unArcArrayObject:%p", [unArcArray firstObject]);
2020-01-19 15:37:31.060712+0800 Demo[37423:6699782] originArray:0x600000d602a0
2020-01-19 15:37:31.060820+0800 Demo[37423:6699782] unArcArray:0x600000d6dec0
2020-01-19 15:37:31.060896+0800 Demo[37423:6699782] originArrayObject:0x1015aa2b0
2020-01-19 15:37:31.060955+0800 Demo[37423:6699782] unArcArrayObject:0xd2186a4844849075
根据打印结果发现,归档解档实现了此类型的深拷贝
自定义类型对象
@interface Person : NSObject <NSCopying>
@property (nonatomic, copy) NSString *name; // 姓名
@end
@implementation Person
- (id)copyWithZone:(NSZone *)zone {
Person *model = [[[self class] allocWithZone:zone] init];
model.name = self.name;
return model;
}
@end
Person *person1 = [[Person alloc] init];
Person *person2 = [person1 copy];
NSLog(@"person1:%p", person1);
NSLog(@"person2:%p", person2);
2020-01-19 16:20:05.178166+0800 Demo[37537:6793237] person1:0x6000025b8230
2020-01-19 16:20:05.181418+0800 Demo[37537:6793237] person2:0x6000025b8380
根据打印结果发现实现了深拷贝
最后结语:1.系统非容器类对象(如NSString类型)当是不可变的类型的时候,copy是浅拷贝,mutableCopy是深拷贝。当是可变类型的时候,copy和mutableCopy都是深拷贝 2.系统容器类型对象 可变和不可变类型的时候copy和mutableCopy都是浅拷贝,可以通过归档解档来实现深拷贝