原文连接html
拷贝是咱们在开发中常常使用的技巧,这里指的不是到Github上去复制粘贴代码,而是对内存中对象的操做 (逃数组
深拷贝与浅拷贝的区别 ? 深拷贝是指咱们拷贝出来的对象拥有本身单独的内存地址,修改新对象不影响源对象,浅拷贝指的是在copy指针的引用,修改新对象会影响到源对象app
在ObjC里面主要有两个方法对对象进行拷贝ui
- (id)copy;
- (id)mutableCopy;
复制代码
要对象可以使用这两个方法须要遵照协议 NSCopying, NSMutableCopyingatom
那么,该什么时候使用这两种方法呢, 先说结论,只有不可变对象调用copy方法的时候才是浅拷贝,其余状况均为深拷贝 新建一个工程验证一下吧spa
Xcode -> New -> MacOS -> CommandLine -> main.m指针
因为NSString 同时实现了 NSCopying, NSMutableCopying 两个协议,咱们就用他来作实验code
NSString *str1 = @"str1";
NSString *str2 = str1.copy;
NSString *str3 = str1.mutableCopy;
NSLog(@"%p %p %p",str1, str2, str3);
复制代码
运行以后能够看到以下输出orm
0x1000020b8 0x1000020b8 0x100508e00
复制代码
由此能够得出结论,不可变对象使用 mutableCopy 为深拷贝 ,copy 为浅拷贝htm
下面验证一下可变对象 NSMutableString
NSMutableString *str1 = [NSMutableString stringWithFormat:@"str1"];
NSString *str2 = str1.copy;
NSString *str3 = str1.mutableCopy;
NSLog(@"%p %p %p",str1, str2, str3);
0x100505260 0x50602036b4ec25f 0x100505290
复制代码
得出结论,可变对象不管使用 copy 仍是 mutableCopy ,均为深拷贝
这些是非集合类型的拷贝结论,那么对于集合类型来讲呢
新建Person类
Person.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface Person : NSObject
@property (nonatomic ,copy) NSString *name;
@property (nonatomic ,assign) NSUInteger age;
@property (nonatomic ,copy) NSArray<Person *> *friends;
@end
NS_ASSUME_NONNULL_END
复制代码
Person.m
#import "Person.h"
@implementation Person
- (instancetype)init {
self = [super init];
if (self) {
_name = @"";
_age = 0;
_friends = @[];
}
return self;
}
// 重写以便打印对象的属性
- (NSString *)description {
return [NSString stringWithFormat:@"- name: %@, age: %ld, friends: %@",self.name, self.age, self.friends];
}
@end
复制代码
若是正常按照上面的来想,集合类型应该也是同样 只有不可变对象调用copy方法的时候才是浅拷贝,其余状况均为深拷贝
下面建立Array来验证想法
Person *Alice = Person.new;
Alice.name = @"Alice";
Alice.age = 18;
NSArray *arr1 = @[Alice];
NSArray *arr2 = arr1.copy;
NSArray *arr3 = arr1.mutableCopy;
NSLog(@"%p %p %p",arr1, arr2, arr3);
0x1030064c0 0x1030064c0 0x103004a90
复制代码
NSArray吻合咱们上面的结论,下面看下NSMutableArray
Person *Alice = Person.new;
Alice.name = @"Alice";
Alice.age = 18;
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:@[Alice]];
NSArray *arr2 = arr1.copy;
NSArray *arr3 = arr1.mutableCopy;
NSLog(@"%p %p %p",arr1, arr2, arr3);
0x1030051f0 0x103006a00 0x1030054a0
复制代码
也温和上面的结论,可是稍等
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:@[Alice]];
NSArray *arr2 = arr1.copy;
NSLog(@"%p %p",arr1.firstObject, arr2.firstObject);
复制代码
咱们能够打印arr1 和 arr2 的第一个元素的地址,发现他们仍是指向同一个地址!
查看Apple关于DeepCopy的文档
咱们发现,原来要对一个集合类型实现真正的深拷贝须要用这种方法, 而上面这种官方名称为【单层深拷贝】
NSArray* trueDeepCopyArray = [NSKeyedUnarchiver unarchiveObjectWithData:
[NSKeyedArchiver archivedDataWithRootObject:oldArray]];
复制代码
好,那么咱们来改造代码
Person *Alice = Person.new;
Alice.name = @"Alice";
Alice.age = 18;
NSError *err;
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:@[Alice]];
NSArray *arr2 = [NSKeyedUnarchiver unarchivedObjectOfClass:[NSArray class] fromData:[NSKeyedArchiver archivedDataWithRootObject:arr1 requiringSecureCoding:false error:&err] error:&err];
if (err) {
NSLog(@"%@",err);
}
NSLog(@"%p %p",arr1.firstObject, arr2.firstObject);
复制代码
奇怪的是输出arr2为nil,看一下控制台输出
-[Person encodeWithCoder:]: unrecognized selector sent to instance 0x100706ac0
复制代码
原来是咱们的Person没有遵照协议 NSCoding , 除此以外咱们还须要遵照 NSSecureCoding 协议
改造后的Person.m
#import "Person.h"
@interface Person () <NSCoding, NSSecureCoding>
@end
@implementation Person
+ (BOOL)supportsSecureCoding {
return true;
}
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:_name forKey:@"name"];
[aCoder encodeInteger:_age forKey:@"age"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super init];
if (self) {
self.name = [aDecoder decodeObjectForKey:@"name"];
self.age = [aDecoder decodeIntegerForKey:@"age"];
}
return self;
}
- (instancetype)init {
self = [super init];
if (self) {
_name = @"";
_age = 0;
_friends = @[];
}
return self;
}
复制代码
如今咱们打开main.m试一下
Person *Alice = Person.new;
Alice.name = @"Alice";
Alice.age = 18;
NSError *err;
NSMutableArray *arr1 = [NSMutableArray arrayWithArray:@[Alice]];
NSArray *arr2 = [NSKeyedUnarchiver unarchivedObjectOfClasses:[NSSet setWithObjects:[NSArray class],[Person class], nil] fromData:[NSKeyedArchiver archivedDataWithRootObject:arr1 requiringSecureCoding:true error:&err] error:&err];
if (err) {
NSLog(@"%@",err);
}
NSLog(@"%p %p",arr1.firstObject, arr2.firstObject);
0x100709880 0x100705c60
复制代码
咱们发现数组里面的对象地址也全都不同了,这才是集合类型的真正深拷贝
thanks.