【发布时间】:2011-05-27 06:03:38
【问题描述】:
我有一个小的类层次结构,我在实现copyWithZone: 时遇到了麻烦。我已阅读 NSCopying 文档,但找不到正确答案。
参加两个课程:Shape 和 Square。正方形定义为:
@interface Square : Shape
这并不奇怪。每个类都有 一个 属性,Shape 有一个“sides” int,Square 有一个“width” int。 copyWithZone: 方法如下所示:
形状
- (id)copyWithZone:(NSZone *)zone {
Shape *s = [[Shape alloc] init];
s.sides = self.sides;
return s;
}
方形
- (id)copyWithZone:(NSZone *)zone {
Square *s = (Square *)[super copyWithZone:zone];
s.width = self.width;
return s;
}
查看文档,这似乎是做事的“正确”方式。
不是。
如果您尝试设置/访问由 copyWithZone: 方法返回的 Square 的 width 属性,它将失败,并出现类似于以下错误的错误:
2010-12-17 11:55:35.441 Hierarchy[22617:a0f] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Shape setWidth:]: unrecognized selector sent to instance 0x10010c970'
在 Square 方法中调用 [super copyWithZone:zone]; 实际上会返回一个 Shape。您甚至可以在该方法中设置 width 属性,这真是个奇迹。
话虽如此,如何以一种不让子类负责复制其超类的变量的方式为子类实现 NSCopying?
【问题讨论】:
标签: objective-c xcode memory-management hierarchy nscopying