通常需要實(shí)現(xiàn)對(duì)模型的拷貝都需要先實(shí)現(xiàn)NSCopying廓译、 NSMutableCopying
協(xié)議,在這里我一直有個(gè)誤區(qū)恶复,以為實(shí)現(xiàn)了copying協(xié)議详羡,數(shù)組使用拷貝操作都會(huì)對(duì)數(shù)組內(nèi)實(shí)現(xiàn)copy協(xié)議的對(duì)象進(jìn)行拷貝。
創(chuàng)建兩個(gè)model并實(shí)現(xiàn)copying協(xié)議
// Dog.h
@interface Dog : NSObject <NSCopying, NSMutableCopying>
@property (nonatomic, copy) NSString *name;
@end
// Dog.m
@implementation Dog
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
Dog *copy = [[Dog allocWithZone:zone]init];
copy.name = self.name;
return copy;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
Dog *copy = [[Dog allocWithZone:zone]init];
copy.name = self.name;
return copy;
}
@end
// Person.h
#import "Dog.h"
@interface Person : NSObject <NSCopying, NSMutableCopying>
@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, copy) Dog *dog;
@end
// Person.m
@implementation Person
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
Person *copy = [[Person allocWithZone:zone]init];
copy.name = self.name;
copy.age = self.age;
copy.dog = [self.dog copy];
return copy;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
Person *copy = [[Person allocWithZone:zone]init];
copy.name = self.name;
copy.age = self.age;
copy.dog = [self.dog copy];
return copy;
}
@end
對(duì)模型數(shù)組使用mutablCopy操作并不是深拷貝
NSMutableArray <Person *>*dataSourceAry = [NSMutableArray new];
for (int i = 0; i < 2; i++) {
Dog *dog = [[Dog alloc]init];
dog.name = @"拉不拉多不多就拉倒";
Person *item = [[Person alloc]init];
item.name = @"杰克";
item.age = 18;
item.dog = [dog copy];
[dataSourceAry addObject:item];
}
NSArray <Person *>*array = [dataSourceAry mutableCopy];
NSLog(@"<dataSourceAry: %@>", dataSourceAry);
NSLog(@"<array: %@>", array);
[array enumerateObjectsUsingBlock:^(Person *obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.name = @"肉絲";
}];
NSLog(@"dataSourceAry[0].name = %@", dataSourceAry[0].name);
NSLog(@"array[0].name = %@", array[0].name);
/** 打印結(jié)果
<dataSourceAry: ("<Person: 0x60000043ec00>", "<Person: 0x60000043e9c0>")>
<array: ("<Person: 0x60000043ec00>","<Person: 0x60000043e9c0>")>
dataSourceAry[0].name = 肉絲
array[0].name = 肉絲
*/
可以從打印的結(jié)果看出,數(shù)組內(nèi)元素的內(nèi)存地址是相同的低零,所以出現(xiàn)了修改拷貝后數(shù)組的第一個(gè)元素婆翔,導(dǎo)致原數(shù)組的第一個(gè)元素也發(fā)生了同樣的改變。雖然兩個(gè)數(shù)組的內(nèi)存地址不一樣掏婶,但是內(nèi)部元素內(nèi)存地址還是同一地址啃奴,不是我們想要的結(jié)果。
實(shí)現(xiàn)模型數(shù)組深拷貝的方法
1雄妥、最笨的方法就是通過遍歷逐個(gè)拷貝元素
NSMutableArray *array = [NSMutableArray array];
for (Person *person in dataSourceAry) {
[array addObject:[person copy]];
}
2最蕾、也有人使用歸檔解檔實(shí)現(xiàn)數(shù)組內(nèi)部元素拷貝
3、這么好用的一個(gè)方法現(xiàn)在才發(fā)現(xiàn)(推薦)
- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag
NSArray <Person *>*deepCopyAry = [[NSArray alloc]initWithArray:dataSourceAry copyItems:YES];
NSLog(@"<dataSourceAry: %@>", dataSourceAry);
NSLog(@"<deepCopyAry: %@>", deepCopyAry);
[deepCopyAry enumerateObjectsUsingBlock:^(Person *obj, NSUInteger idx, BOOL * _Nonnull stop) {
obj.name = @"弗蘭克";
obj.dog.name = @"弗蘭克的dog";
}];
NSLog(@"dataSourceAry[0].name = %@", dataSourceAry[0].name);
NSLog(@"deepCopyAry[0].name = %@", deepCopyAry[0].name);
NSLog(@"dataSourceAry[0].dog.name = %@", dataSourceAry[0].dog.name);
NSLog(@"deepCopyAry[0].dog.name = %@", deepCopyAry[0].dog.name);
/** 打印結(jié)果
<dataSourceAry: ("<Person: 0x604000427680>", "<Person: 0x604000425220>")>
<deepCopyAry: ("<Person: 0x60000042cb80>", "<Person: 0x60000042cae0>")>
dataSourceAry[0].name = 肉絲
deepCopyAry[0].name = 弗蘭克
dataSourceAry[0].dog.name = 拉不拉多不多就拉倒
deepCopyAry[0].dog.name = 弗蘭克的dog
*/
總結(jié)
1老厌、模型數(shù)組內(nèi)元素中模型必須要實(shí)現(xiàn)copying協(xié)議瘟则,模型內(nèi)如果有嵌套模型,也需要實(shí)現(xiàn)copying協(xié)議枝秤,否則執(zhí)行對(duì)對(duì)象拷貝操作會(huì)出現(xiàn)崩潰醋拧;
2、使用- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag
實(shí)現(xiàn)模型數(shù)組拷貝淀弹;