Understanding copy in the Objective-c

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.