I have a small class hierarchy that I\'m having trouble implementing copyWithZone:
for. I\'ve read the NSCopying documentation, and I can\'t find the correct an
One of those things you realize right after asking...
The implementation of copyWithZone:
in the superclass (Shape) shouldn't be assuming it's a Shape. So instead of the wrong way, as I mentioned above:
- (id)copyWithZone:(NSZone *)zone {
Shape *s = [[Shape allocWithZone:zone] init];
s.sides = self.sides;
return s;
}
You should instead use:
- (id)copyWithZone:(NSZone *)zone {
Shape *s = [[[self class] allocWithZone:zone] init]; // <-- NOTE CHANGE
s.sides = self.sides;
return s;
}