I have a class, MyClass, which implements the NSCopying protocol, and I have a class, MyClassChild, which inherits from MyClass. MyClassChild does not implement the NSCopying protocol. The textbook I am reading says it must, however I can build successfully! Is the textbook wrong?
@interface MyClass : NSObject <NSCopying> {
}
@end
@implementation MyClass
-(id)copyWithZone:(NSZone *)zone
{
return self;
}
@end
@interface MyClassChild : MyClass {
}
@end
@implementation MyClassChild
@end
When you don’t supply an implementation for the
copyWithZone:method inMyClassChild, the class inherits the method implementation from its superclass (MyClass). This means thatMyClassChilddoes conform to theNSCopyingprotocol. IfMyClassChildneeds to do something special when its instances are being copied, you should overridecopyWithZone:and do whatever needs to be done there. Hope that helps.P.S. I hope you realize that returning
selfis not a good way to implementcopyWithZone:?