Whether an instance object in Objective-c supports a copy operation depends on whether the object implements the Nscopying protocol:
@protocol nscopying-(ID) Copywithzone: (Nszone *) zone; @end
For mutablecopy operations, there is a nsmutablecopying protocol:
@protocol nsmutablecopying-(ID) Mutablecopywithzone: (Nszone *) zone; @end
We see that there are copy and Mutablecopy methods in the NSObject interface, is it wrong to say that we must follow the agreement above?
@interface NSObject <NSObject> { Class isa objc_isa_availability;} ........ -(ID) copy; -(ID) mutablecopy; ....... @end
Let's do a test:
// test nsobject copy NSObject *originobject = [[NSObject alloc] init]; *copiedobject = [originobject copy]; *mutablecopiedobject = [Originobject mutablecopy]; NSLog (@ "%@,%@,%@", Originobject, Copiedobject, Mutablecopiedobject);
When the code executes to [originobject copy] crash, it is suggested that NSObject does not implement the Nscopying protocol method,
£ º56.680 nslocktest[52535: 8160470x79f909b0
Understanding copy in the Objective-c