iOS 模型数组深拷贝

通常需要实现对模型的拷贝都需要先实现NSCopying、 NSMutableCopying协议,在这里我一直有个误区,以为实现了copy协议,数组使用拷贝操作都会对数组内实现copy协议的对象进行拷贝。

创建两个model并实现copying协议

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 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

对模型数组使用mutablCopy操作并不是深拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
/** 打印结果
<dataSourceAry: ("<Person: 0x60000043ec00>", "<Person: 0x60000043e9c0>")>
<array: ("<Person: 0x60000043ec00>","<Person: 0x60000043e9c0>")>
dataSourceAry[0].name = 肉丝
array[0].name = 肉丝
*/

可以从打印的结果看出,数组内元素的内存地址是相同的,所以出现了修改拷贝后数组的第一个元素,导致原数组的第一个元素也发生了同样的改变。虽然两个数组的内存地址不一样,但是内部元素内存地址还是同一地址,不是我们想要的结果。

实现模型数组深拷贝的方法

1、最笨的方法就是通过遍历逐个拷贝元素

1
2
3
4
NSMutableArray *array = [NSMutableArray array];
for (Person *person in dataSourceAry) {
[array addObject:[person copy]];
}

2、也有人使用归档解档实现数组内部元素拷贝

3、这么好用的一个方法现在才发现(推荐)

flag=YES当模型数组内元素对象遵守了copy协议,数组元素对象才能进行深拷贝。数组元素对象没有遵守copy协议或者flag=NO,则会进行浅拷贝

1
- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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);
/** 打印结果
<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
*/

总结

1、模型数组内元素中模型必须要实现copy协议,模型内如果有嵌套模型,也需要实现copy协议,否则执行对对象拷贝操作会出现崩溃;
2、使用- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag实现模型数组拷贝;