我想澄清一下关于实现copyWithZone:的几件事,有谁能评论一下.
// 001: Crime is a subclass of NSObject.
- (id)copyWithZone:(NSZone *)zone {
Crime *newCrime = [[[self class] allocWithZone:zone] init];
if(newCrime) {
[newCrime setMonth:[self month]];
[newCrime setCategory:[self category]];
[newCrime setCoordinate:[self coordinate]];
[newCrime setLocationName:[self locationName]];
[newCrime setTitle:[self title]];
[newCrime setSubtitle:[self subtitle]];
}
return newCrime;
}
// 002: Crime is not a subclass of NSObject.
- (id)copyWithZone:(NSZone *)zone {
Crime *newCrime = [super copyWithZone:zone];
[newCrime setMonth:[self month]];
[newCrime setCategory:[self category]];
[newCrime setCoordinate:[self coordinate]];
[newCrime setLocationName:[self locationName]];
[newCrime setTitle:[self title]];
[newCrime setSubtitle:[self subtitle]];
return newCrime;
}在001中:
[[[self Class] allocWithZone:zone] init]
[[Crime allocWithZone:zone] init]还是应该使用[[Crime allocWithZone:zone] init]它可以使用[self month]复制iVars还是应该直接访问iVars,即_month发布于 2012-03-28 13:02:25
[[self class] allocWithZone:zone]来确保使用适当的类创建副本。您给出的002示例确切地说明了为什么:子类将调用[super copyWithZone:zone],并期望得到适当类的实例,而不是超类的实例。代码:
- (id)copyWithZone:(NSZone *)zone {
Crime *newCrime = [super copyWithZone:zone];
newCrime->_month = [_month copyWithZone:zone];
newCrime->_category = [_category copyWithZone:zone];
// etc...
return newCrime;
}当然,不管你是复制象牙,保留它们,还是仅仅分配它们,都应该反映出设置者所做的事情。
发布于 2015-04-15 15:06:51
使用SDK提供的对象的copyWithZone:方法的默认复制行为是“浅拷贝”。这意味着如果在copyWithZone:对象上调用NSString,它将创建一个浅拷贝,而不是深拷贝。浅拷贝和深拷贝的区别是:
对象的浅拷贝将只复制对原始数组的对象的引用,并将它们放置到新数组中。
深度复制将实际复制包含在对象中的各个对象。这是通过在自定义类方法中向每个单独的对象发送copyWithZone:消息来完成的。
简而言之:要获得浅拷贝,您可以在所有实例变量上调用retain或strong。要获得深度复制,您可以对自定义类copyWithZone:实现中的所有实例变量调用copyWithZone:。现在你可以选择了。
发布于 2017-08-08 03:18:16
那么这个实现深度复制的呢:
/// Class Foo has two properties: month and category
- (id)copyWithZone:(NSZone *zone) {
Foo *newFoo;
if ([self.superclass instancesRespondToSelector:@selector(copyWithZone:)]) {
newFoo = [super copyWithZone:zone];
} else {
newFoo = [[self.class allocWithZone:zone] init];
}
newFoo->_month = [_month copyWithZone:zone];
newFoo->_category = [_category copyWithZone:zone];
return newFoo;
}https://stackoverflow.com/questions/9907154
复制相似问题