iOS 容器的浅拷贝和深拷贝
2017-12-25 本文已影响0人
GlassHead
1.不可变容器
NSArray *array = [NSArray array];
NSLog(@"拷贝前:%p\n拷贝后:%p",array,[array copy]);
2017-12-25 14:06:08.061639+0800 RAC_MVVM[97090:6343605] 拷贝前:0x60000000ac40
拷贝后:0x60000000ac40
NSArray *array = [NSArray array];
NSLog(@"拷贝前:%p\n拷贝后:%p",array,[array mutableCopy]);
2017-12-25 14:09:06.045388+0800 RAC_MVVM[97309:6348491] 拷贝前:0x600000007a30 拷贝后:0x60400025b000
不可变容器的copy是浅拷贝,mutableCopy为深拷贝
2.可变容器
NSMutableArray *mutableArray = [NSMutableArray array];
NSLog(@"拷贝前:%p\n拷贝后:%p",mutableArray,[mutableArray copy]);
2017-12-25 14:10:58.338086+0800 RAC_MVVM[97373:6350541] 拷贝前:0x600000440960
拷贝后:0x600000001ee0
NSMutableArray *mutableArray = [NSMutableArray array];
NSLog(@"拷贝前:%p\n拷贝后:%p",mutableArray,[mutableArray mutableCopy]);
2017-12-25 14:11:39.924935+0800 RAC_MVVM[97443:6351719] 拷贝前:0x604000249120
拷贝后:0x604000249030
可变容器的copy和mutableCopy都是深拷贝。
总结:执行copy或者mutableCopy前后都是不可变容器的是浅拷贝,否则是深拷贝。