对于iOS系统对象的复制可以参考以下规则:
- 可变对象的copy和mutableCopy方法都是深拷贝(区别完全深拷贝与单层深拷贝).
- 不可变对象的copy方法是浅拷贝,mutableCopy方法是深拷贝.
- copy方法返回的对象都是不可变对象
自定义对象的深拷贝
系统对象由于有可变和不可变之分,浅拷贝和深拷贝有点复杂.自定义对象没有什么可变不可变的概念,因此,只讨论深拷贝的情况.
自定义对象实现拷贝需要签订NSCopying或者NSMutableCopying协议,并实现copyWithZone:或者mutableCopyWithZone:.它们的实现方法类似,这里就只讲copyWithZone:
首先,自定义类需要签订<NSCopying或者NSMutableCopying>协议.
先创建一个自定义类Person,并且签订<NSCopying>协议.
@interface Person : NSObject<NSCopying>
@property (nonatomic, strong) NSString *name;//语义属性暂时用strong
@property (nonatomic, assign) NSInteger age;
@end
然后实现对应的copyWithZone:方法,在实现中使用allocWithZone:方法创建一个新的对象,然后给属性赋值.属性在赋值的时候也要深拷贝一下,否则属性就是浅拷贝的,对象的深拷贝也就不完整了.完成这些后,在使用的时候就可以直接调用这个对象的copy方法了(代码如下:)
@implementation Person
- (id)copyWithZone:(NSZone *)zone {
Person *p = [[Person allocWithZone:zone] init];
//属性也要拷贝赋值
p.name = [self.name mutableCopy];
p.age = self.age;
return p;
}
@end
@implementation ViewController
Person *p = [[Person alloc] init];
p.name = [NSString stringWithFormat:@"%@", @"黄继业"];
Person *copyP = [p copy];
NSLog(@"p = %p, copyP = %p", p, copyP);
NSLog(@"p.name = %p, copyP.name = %p", p.name, copyP.name);
@end
输出结果为:
p = 0x60000002a080, copyP = 0x600000034020
p.name = 0x60000002f480, copyP.name = 0x600000252f00
如果Person对象中又有其他的自定义对象属性,那么这个对象也需要实现它的copyWithZone:方法.(代码如下:)
@implementation Dog
- (id)copyWithZone:(NSZone *)zone {
Dog *d = [[[self class] allocWithZone:zone] init];
return d;
}
@end
@interface Person : NSObject<NSCopying>
@property (nonatomic, strong) NSString *name;//语义属性暂时用strong
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, strong) Dog *dog;//新增的自定义对象属性
@end
@implementation Person
- (id)copyWithZone:(NSZone *)zone {
Person *p = [[[self class] allocWithZone:zone] init];
//name属性不可变的话,或者语义属性为copy的话,都可以直接赋值了
p.name = [self.name mutableCopy];
p.age = self.age;
//使用自定义对象的copy方法
p.dog = [self.dog copy];
return p;
}
@end
@implementation ViewController
Person *p = [[Person alloc] init];
p.name = [NSString stringWithFormat:@"%@", @"黄继业"];
p.dog = [[Dog alloc] init];
Person *copyP = [p copy];
NSLog(@"p = %p, copyP = %p", p, copyP);
NSLog(@"p.name = %p, copyP.name = %p", p.name, copyP.name);
NSLog(@"p.dog = %p, copyP.dog = %p", p.dog, copyP.dog);
@end
输出结果为:
p = 0x6000000340c0, copyP = 0x60000003e340
p.name = 0x60000003e1e0, copyP.name = 0x600000445d30
p.dog = 0x60000003e320, copyP.dog = 0x60000003e200
之所以签订协议,是为了给这个类以及它的子类添加统一的copy方法.否则我们完全可以简单粗暴的自己写个方法去实现这个对象的拷贝.比如我们创建一个子类黄种人YPerson,只要重写copyWithZone:方法即可:
@implementation YPerson
- (id)copyWithZone:(NSZone *)zone {
YPerson *yp = [super copyWithZone:zone];
//给子类特有的属性赋值
yp.address = [self.address mutableCopy];
return yp;
}
@end
@implementation ViewController
YPerson *yp = [[YPerson alloc] init];
YPerson *copyYP = [yp copy];
NSLog(@"yp = %p, copyYP = %p", yp, copyYP);
@end
输出结果为:
yp = 0x600000249e40, copyYP = 0x600000249a50
网友评论