iOS - 如何深拷贝Array内元素、自定义对象、及自定义对象

2021-05-09  本文已影响0人  码代码的小马

关于深拷贝、浅拷贝,请看上篇iOS - 深拷贝、浅拷贝探索验证

关于深拷贝、浅拷贝,请看上篇iOS - 深拷贝、浅拷贝探索验证

  1. 上篇我们知道:
  • copy: 对NSArray是浅拷贝,NSMutableArray是深拷贝
  • mutableCopy: 始终是深拷贝
  • 无论深浅拷贝,集合对象内元素都是浅拷贝
  1. 本篇主要探索
  • 实现NSArray内元素的拷贝

通过本文你将知道

  • 深拷贝NSArray
  • 深拷贝NSArray内元素Person
  • 深拷贝NSArray内元素Person的属性nickname

本文代码较多,建议一步步耐心跟着代码排查,会有不少收货

探索过程

1. 系统Api

我们发现系统系统了方法- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag
其中copyItems:(BOOL)flag表示是否拷贝元素,我们先代码测试一下

    NSString *str1 = @"hello world";
    NSMutableString *str2 = [NSMutableString stringWithString:@"hello world"];
    NSArray *array1 = [NSArray arrayWithObjects: str1, str2, nil];
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithArray:array1 copyItems:true];

    NSLog(@"\n array1 = %p class = %@ \n", array1, [array1 class]);
    NSLog(@"\n array2 = %p class = %@ \n", array2, [array2 class]);

    NSLog(@"\n\n======== 元素是String ======== ");
    NSLog(@"\n obj0 = %p class = %@ \n", array1[0], [array1[0] class]);
    NSLog(@"\n obj0 = %p class = %@ \n", array2[0], [array2[0] class]);

    NSLog(@"\n\n======== 元素是mutableString ========");
    NSLog(@"\n obj0 = %p class = %@ \n", array1[1], [array1[1] class]);
    NSLog(@"\n obj0 = %p class = %@ \n", array2[1], [array2[1] class]);

查看输出(为了下文对比,我对输出做了line标记)

2021-05-09 10:59:16.343163+0800 AlgorithmDemo[9904:51756] 
 array1 = 0x100607650 class = __NSArrayI  (line1)
 array2 = 0x1006074a0 class = __NSArrayM (line2)

======== 元素是String ======== 
 obj0 = 0x100008220 class = __NSCFConstantString (line3)
 obj0 = 0x100008220 class = __NSCFConstantString (line4)

======== 元素是mutableString ========
 obj0 = 0x100606330 class = __NSCFString (line5)
 obj0 = 0x100605430 class = __NSCFString (line6)

根据输出我们对得出系统initWithArray : copyItems :方法如下结论

由此我们知道,当我们想深拷贝NSArray及其内部的元素时,用系统方法- (instancetype)initWithArray:(NSArray<ObjectType> *)array copyItems:(BOOL)flag是不行的

2. 归档

如果我们对Array执行归档再解档,结果会是什么样的呢,我们代码测试一下

    NSString *str1 = @"hello world";
    NSMutableString *str2 = [NSMutableString stringWithString:@"hello world"];
    NSArray *array1 = [NSArray arrayWithObjects: str1, str2, nil];
    NSMutableArray *array2 = [[NSMutableArray alloc] initWithArray:array1 copyItems:true];
    
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array1];
    NSMutableArray *array3 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    NSLog(@"\n array1 = %p class = %@ \n", array1, [array1 class]);
    NSLog(@"\n array2 = %p class = %@ \n", array2, [array2 class]);
    NSLog(@"\n array3 = %p class = %@ \n", array3, [array3 class]);
    
    NSLog(@"\n\n======== 元素是String ======== ");
    NSLog(@"\n obj0 = %p class = %@ \n", array1[0], [array1[0] class]);
    NSLog(@"\n obj0 = %p class = %@ \n", array2[0], [array2[0] class]);
    NSLog(@"\n obj0 = %p class = %@ \n", array3[0], [array3[0] class]);
    
    NSLog(@"\n\n======== 元素是mutableString ========");
    NSLog(@"\n obj0 = %p class = %@ \n", array1[1], [array1[1] class]);
    NSLog(@"\n obj0 = %p class = %@ \n", array2[1], [array2[1] class]);
    NSLog(@"\n obj0 = %p class = %@ \n", array3[1], [array3[1] class]);

查看输出

2021-05-09 11:14:50.710409+0800 AlgorithmDemo[12261:67019] 
 array1 = 0x1006b9e40 class = __NSArrayI  (line1)
 array2 = 0x1006ba180 class = __NSArrayM  (line1)
 array3 = 0x1006b7410 class = __NSArrayI  (line1)

======== 元素是String ======== 
 obj0 = 0x100008220 class = __NSCFConstantString  (line1)
 obj0 = 0x100008220 class = __NSCFConstantString  (line1)
 obj0 = 0x1006b6d20 class = __NSCFString  (line1)

======== 元素是mutableString ========
 obj0 = 0x1006b9cb0 class = __NSCFString  (line1)
 obj0 = 0x1006ba0e0 class = __NSCFString  (line1)
 obj0 = 0x1006b70e0 class = __NSCFString  (line1)

根据输出,我们很明显得出如下结论

由此,我们得出对如下总结

3. 重写copyWithZone mutableCopyWithZone的方式深拷贝自定义对象

我们写一个person类测试一下

//
//  Person.h
//  AlgorithmDemo
//
//  Created by Ternence on 2021/5/9.
//

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, copy) NSString *nickname;

@end

测试代码如下

    Person *person = [[Person alloc] init];
    person.nickname = @"码代码的小马";
    
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:person, nil];
    
    NSArray *array2 = [array1 copy];
    NSMutableArray *array3 = [array1 mutableCopy];
    
    NSLog(@"\n array1 = %p class = %@", array1, [array1 class]);
    NSLog(@"\n array2 = %p class = %@", array2, [array2 class]);
    NSLog(@"\n array3 = %p class = %@", array3, [array3 class]);
    
    NSLog(@"\n\n======== 数组内元素 ======== ");
    NSLog(@"\n array1[0] = %p class = %@", array1[0], [array1[0] class]);
    NSLog(@"\n array2[0] = %p class = %@", array2[0], [array2[0] class]);
    NSLog(@"\n array3[0] = %p class = %@", array3[0], [array3[0] class]);

查看输出

2021-05-09 11:33:47.241685+0800 AlgorithmDemo[17319:89117] 
 array1 = 0x102a04450 class = __NSArrayM
 array2 = 0x102a04550 class = __NSSingleObjectArrayI
 array3 = 0x102a04570 class = __NSArrayM

======== 数组内元素 ======== 
 array1[0] = 0x102a042c0 class = Person
 array2[0] = 0x102a042c0 class = Person
 array3[0] = 0x102a042c0 class = Person

我们发现数组对象已经被深拷贝,但数组对象内的元素还是浅拷贝

是不是我们在定义Person类的时候没有重写方法呢? 我们把改一下Person类的代码,并对NSMutableArray内元素进行copy

! 此处的copyWithZone mutableCopyWithZone方法里,我们应该对属性也进行copy

//
//  Person.m
//  AlgorithmDemo
//
//  Created by Ternence on 2021/5/9.
//

#import "Person.h"

@interface Person ()<NSCopying, NSMutableCopying, NSCoding>

@end

@implementation Person

- (id)copyWithZone:(NSZone *)zone {
    Person *person = [Person allocWithZone:zone];
    person.nickname = [self.nickname copy];
    return person;
}

- (id)mutableCopyWithZone:(NSZone *)zone {
    Person *person = [Person allocWithZone:zone];
    person.nickname = [self.nickname mutableCopy];
    return person;
}

@end

测试代码

    Person *person = [[Person alloc] init];
    person.nickname = @"码代码的小马";
    
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:person, nil];
    
    NSArray *array2 = [array1 copy];
    NSMutableArray *array3 = [array1 mutableCopy];
    NSMutableArray *array4 = [[NSMutableArray alloc] initWithArray:array1 copyItems:true];
    NSMutableArray *array5 = [[NSMutableArray alloc] initWithObjects:[array1[0] copy], nil];
    NSMutableArray *array6 = [[NSMutableArray alloc] initWithObjects:[array1[0] mutableCopy], nil];
    
    NSLog(@"\n array1 = %p class = %@", array1, [array1 class]);
    NSLog(@"\n array2 = %p class = %@", array2, [array2 class]);
    NSLog(@"\n array3 = %p class = %@", array3, [array3 class]);
    NSLog(@"\n array4 = %p class = %@", array4, [array4 class]);
    NSLog(@"\n array5 = %p class = %@", array5, [array5 class]);
    NSLog(@"\n array6 = %p class = %@", array6, [array6 class]);
    
    NSLog(@"\n\n======== 数组内元素 ========");
    Person *orgArrayObj = (Person *)array1[0];
    Person *newArrayObj2 = (Person *)array2[0];
    Person *newArrayObj3 = (Person *)array3[0];
    Person *newArrayObj4 = (Person *)array4[0];
    Person *newArrayObj5 = (Person *)array5[0];
    Person *newArrayObj6 = (Person *)array6[0];
    NSLog(@"\n array1[0] = %p nickname = %p", orgArrayObj, orgArrayObj.nickname);
    NSLog(@"\n array2[0] = %p nickname = %p", newArrayObj2, newArrayObj2.nickname);
    NSLog(@"\n array3[0] = %p nickname = %p", newArrayObj3, newArrayObj3.nickname);
    NSLog(@"\n array4[0] = %p nickname = %p", newArrayObj4, newArrayObj4.nickname);
    NSLog(@"\n array5[0] = %p nickname = %p", newArrayObj5, newArrayObj5.nickname);
    NSLog(@"\n array6[0] = %p nickname = %p", newArrayObj6, newArrayObj6.nickname);

继续查看打印结果

2021-05-10 11:18:25.020830+0800 AlgorithmDemo[98320:429537] 
 array1 = 0x10041a790 class = __NSArrayM
 array2 = 0x100419b60 class = __NSSingleObjectArrayI
 array3 = 0x10041a880 class = __NSArrayM
 array4 = 0x10041a9f0 class = __NSArrayM
 array5 = 0x10041aa20 class = __NSArrayM
 array6 = 0x10041ac70 class = __NSArrayM

======== 数组内元素 ========
 array1[0] = 0x10041a340 nickname = 0x1000083a0 
 array2[0] = 0x10041a340 nickname = 0x1000083a0
 array3[0] = 0x10041a340 nickname = 0x1000083a0
 array4[0] = 0x100417660 nickname = 0x1000083a0 
 array5[0] = 0x100415370 nickname = 0x1000083a0
 array6[0] = 0x100412300 nickname = 0x10041ad20

我们再根据打印归纳总结一下
对于重写copyWithZone:mutableCopyWithZone:的自定义对象

此时我们知道了,要对自定义对象进行深拷贝,解决方案是重写copyWithZonemutableCopyWithZone,调用对应的方法即可

4. 用归档的方式深拷贝自定义对象

先改一下person类

//
//  Person.m
//  AlgorithmDemo
//
//  Created by Ternence on 2021/5/9.
//

#import "Person.h"

@interface Person ()<NSCopying, NSMutableCopying, NSCoding>

@end

@implementation Person

- (instancetype)initWithCoder:(NSCoder *)coder {
    self.nickname = [coder decodeObjectForKey:@"nickname"];
    return  self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.nickname forKey:@"nickname"];
}

@end

再改写测试代码

    Person *person = [[Person alloc] init];
    person.nickname = @"码代码的小马";
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:person, nil];
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array1];
    NSMutableArray *array2 = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    NSLog(@"\n array1 = %p class = %@", array1, [array1 class]);
    NSLog(@"\n array2 = %p class = %@", array2, [array2 class]);
    
    NSLog(@"\n\n======== 数组内元素 ========");
    Person *orgArrayObj = (Person *)array1[0];
    Person *newArrayObj2 = (Person *)array2[0];
    NSLog(@"\n array1[0] = %p nickname = %p", orgArrayObj, orgArrayObj.nickname);
    NSLog(@"\n array2[0] = %p nickname = %p", newArrayObj2, newArrayObj2.nickname);

再耐心查看一下打印

2021-05-10 11:41:52.420699+0800 AlgorithmDemo[2252:455650] 
 array1 = 0x103b04e50 class = __NSArrayM
 array2 = 0x103b06f30 class = __NSArrayM

======== 数组内元素 ========
 array1[0] = 0x10061dcc0 nickname = 0x1000083a0
 array2[0] = 0x103b05860 nickname = 0x103b06120
 

很明显,array对象深拷贝了,array内的自定义对象person深拷贝了,自定义对象personnickname也深拷贝了

我们来对Array的拷贝做个年终总结:

上一篇下一篇

猜你喜欢

热点阅读