概念解释:
浅拷贝:
1.指针拷贝,没有开辟新的内存;
2.生成一个新的指针变量指向原有对象的地址;
3.原有对象引用计数+1。
深拷贝:
1.从堆中开辟一份新的内存给新的对象,并将原有对象的信息赋值给新的对象;
2.生成一个新的指针变量指向新的对象;
3.原有对象引用计数不会增加,新的对象引用计数为1。
完全拷贝:
1.是对多层对象包含或者集合中存储对象而言的;
2.被拷贝对象的每一层都进行了深拷贝;
3.例如集合中的对象,对象中的对象等。
非集合不可变类:
示例:
- (void)stringCopy {
NSString * a = @"zby-haha-heihei-123456";
NSString * a1 = [a copy];
NSString * a2 = [a mutableCopy];
NSLog(@"%p-%p-%p", a, a1, a2);
NSLog(@"%@-%@-%@", a.class, a1.class, a2.class);
NSObject * o = [[NSObject alloc] init];
NSObject * o1 = [o copy];
NSObject * o2 = [o mutableCopy];
NSLog(@"%p-%p-%p", o, o1, o2);
NSLog(@"%@-%@-%@", o.class, o1.class, o2.class);
}
打印输出:
0x100bf11c8-0x100bf11c8-0x2823e1ad0
__NSCFConstantString-__NSCFConstantString-__NSCFString
结论:
- 对非集合不可变对象的copy为浅拷贝,拷贝的指针指向原有对象地址;
- 对非集合不可变对象的mutableCopy为深拷贝,复制的对象为可变对象。
非集合可变类:
示例:
- (void)mutableStringCopy {
NSMutableString * a = [[NSMutableString alloc] initWithFormat:@"zby-haha-heihei-123456"];
NSMutableString * a1 = [a copy];
NSMutableString * a2 = [a mutableCopy];
NSLog(@"%p-%p-%p", a, a1, a2);
NSLog(@"%@-%@-%@", a.class, a1.class, a2.class);
NSMutableString * b = [NSMutableString stringWithString:@"非集合可变类"];
NSMutableString * b1 = [a copy];
NSMutableString * b2 = [a mutableCopy];
NSLog(@"%p-%p-%p", b, b1, b2);
NSLog(@"%@-%@-%@", b.class, b1.class, b2.class);
}
打印输出:
0x2823e2070-0x2823e1bf0-0x2823e1ad0
__NSCFString-__NSCFString-__NSCFString
0x2823e2610-0x2823e1f20-0x2823e01b0
__NSCFString-__NSCFString-__NSCFString
结论:
- 对非集合可变对象的copy为深拷贝,复制的对象为可变对象;
- 对非集合可变对象的mutableCopy为深拷贝,复制的对象为可变对象。
集合不可变类:
示例:
- (void)containerCopy {
NSArray * a = @[@"集合不可变类", @"集合可变类"];
NSArray * a1 = [a copy];
NSArray * a2 = [a mutableCopy];
NSLog(@"%p-%p-%p", a, a1, a2);
NSLog(@"%@-%@-%@", a.class, a1.class, a2.class);
NSDictionary * dic = @{@"key" : @"value"};
NSDictionary * dic1 = [dic copy];
NSDictionary * dic2 = [dic mutableCopy];
NSLog(@"%p-%p-%p", dic, dic1, dic2);
NSLog(@"%@-%@-%@", dic.class, dic1.class, dic2.class);
NSSet * set = [NSSet setWithObjects:@"set集合", @12, nil];
NSSet * set1 = [set copy];
NSSet * set2 = [set mutableCopy];
NSLog(@"%p-%p-%p", set, set1, set2);
NSLog(@"%@-%@-%@", set.class, set1.class, set2.class);
NSLog(@"%p-%p-%p", a[0], a1[0], a2[0]);
}
打印输出:
0x282d24720-0x282d24720-0x2823e19e0
__NSArrayI-__NSArrayI-__NSArrayM
0x282d24880-0x282d24880-0x282d24640
__NSSingleEntryDictionaryI-__NSSingleEntryDictionaryI-__NSDictionaryM
0x2823e1b30-0x2823e1b30-0x282d245a0
__NSSetI-__NSSetI-__NSSetM
0x100bf1128-0x100bf1128-0x100bf1128
结论:
- 对集合不可变对象的copy为浅拷贝,拷贝的指针指向原有对象地址;
- 对集合不可变对象的mutableCopy为深拷贝,复制的对象为可变对象。
集合可变类:
示例:
- (void)mutableContainerCopy {
NSMutableArray * a = [NSMutableArray arrayWithArray:@[@"集合不可变类", @"集合可变类"]];
NSMutableArray * a1 = [a copy];
NSMutableArray * a2 = [a mutableCopy];
NSLog(@"%p-%p-%p", a, a1, a2);
NSLog(@"%@-%@-%@", a.class, a1.class, a2.class);
NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithDictionary:@{@"key" : @"value"}];
NSMutableDictionary * dic1 = [dic copy];
NSMutableDictionary * dic2 = [dic mutableCopy];
NSLog(@"%p-%p-%p", dic, dic1, dic2);
NSLog(@"%@-%@-%@", dic.class, dic1.class, dic2.class);
NSMutableSet * set = [NSMutableSet setWithObjects:@"set集合", @12, nil];
NSMutableSet * set1 = [set copy];
NSMutableSet * set2 = [set mutableCopy];
NSLog(@"%p-%p-%p", set, set1, set2);
NSLog(@"%@-%@-%@", set.class, set1.class, set2.class);
NSLog(@"%p-%p-%p", a[0], a1[0], a2[0]);
}
打印输出:
0x2823e8a20-0x282d78580-0x2823e8270
__NSArrayM-__NSArrayI-__NSArrayM
0x282d78360-0x282d79900-0x282d79a00
__NSDictionaryM-__NSFrozenDictionaryM-__NSDictionaryM
0x282d799a0-0x2823e8750-0x282d79780
__NSSetM-__NSSetI-__NSSetM
0x100bf1128-0x100bf1128-0x100bf1128
结论:
- 对集合可变对象的copy为深拷贝,复制的对象为不可变对象;
- 对集合可变对象的mutableCopy为深拷贝,复制的对象为可变对象;
- 集合类可变对象mutableCopy和copy都返回一个新的集合,但集合内的元素仍然是浅拷贝。
总结:
- 不可变对象copy的都是浅拷贝,拷贝的指针指向原有对象地址;
- 可变对象copy都是深拷贝,可变集合copy后的复制对象为不可变集合;
- mutableCopy的都是深拷贝,且复制的对象为可变对象。
12345.png
自定义类的拷贝和NSCoping协议:
在OC中不是所有的类都支持拷贝,只有遵循NSCopying
才支持copy,只有遵循NSMutableCopying
才支持mutableCopy。
NSObject有一个实列方法叫做- (id)copy
。在OC源码中可以看到,默认的copy方法调用为:
- (id)copy {
return [(id)self copyWithZone:nil];
}
对于采纳了NSCopying
协议的子类,需要实现copyWithZone
方法,否则会引发异常。NSMutableCopying
只有在MRC模式下使用。
示例:
// ZBYCopyPerson.h
@interface ZBYCopyPerson : NSObject <NSCopying>
/// 名字
@property (nonatomic, copy) NSString * name;
/// 年龄
@property (nonatomic, assign) NSInteger age;
@end
// ZBYCopyPerson.m
@implementation ZBYCopyPerson
- (nonnull id)copyWithZone:(nullable NSZone *)zone {
ZBYCopyPerson * p = [[ZBYCopyPerson allocWithZone:zone] init];
p.name = self.name;
p.age = self.age;
return p;
}
//- (nonnull id)mutableCopyWithZone:(nullable NSZone *)zone {
// ZBYCopyPerson * p = [[ZBYCopyPerson mutableCopyWithZone:zone] init];
// p.name = self.name;
// p.age = self.age;
// return p;
//}
@end
- (void)customClassCopy {
ZBYCopyPerson * p = [ZBYCopyPerson new];
p.name = @"器不可变类";
p.age = 23;
ZBYCopyPerson * p1 = [p copy];
NSLog(@"%p-%p", p, p1);
NSArray * pList = @[p];
NSArray * pCopyList = [NSArray arrayWithArray:pList];
NSArray * pMCopyList = [[NSArray alloc] initWithArray:pList copyItems:YES];
NSLog(@"%p-%p-%p", pList, pCopyList, pMCopyList);
NSLog(@"%p-%p-%p", pList.firstObject, pCopyList.firstObject, pMCopyList.firstObject);
}
结论:
自定义类通过重写copyWithZone
方法实现了深拷贝,通过copy
方法(该方法默认调用copyWithZone
方法)复制得到p1,从结果可以看出:深复制对象和和源对象的地址是不一样的。