I want to allow deep copy of my class object and am trying to implement copyWithZone but the call to [super copyWithZone:zone] yields the error:
error: no visible @interface for 'NSObject' declares the selector 'copyWithZone:'
@interface MyCustomClass : NSObject
@end
@implementation MyCustomClass
- (id)copyWithZone:(NSZone *)zone
{
// The following produces an error
MyCustomClass *result = [super copyWithZone:zone];
// copying data
return result;
}
@end
How should I create a deep copy of this class?
You should add the
NSCopyingprotocol to your class's interface.Then the method should be:
NSObjectdoesn't conform to theNSCopyingprotocol. This is why you can't callsuper copyWithZone:.Edit: Based on Roger's comment, I have updated the first line of code in the
copyWithZone:method. But based on other comments, the zone can safely be ignored.