22nd: Understanding the Nscopying Protocol

Source: Internet
Author: User

If you want a custom class to support copy operations, implement the nscopying protocol (not the replication Copy method) or nsmutablecopying protocol.

Copies of immutable versions:

Nscopying protocol, the Protocol has only one method:

-(ID) Copywithzone: (nszone*) zone;

In the past, when the program was developed, the memory is divided into different"zones", and objects are created in a certain area. Not now, each program has only one zone: " default Zone ".

Cases:

-(ID)copywithzone:(nszone*) zone {

Person *copy = [[[Self class]allocwithzone:zone]initwithfirstname:_firstname andlastname:_lastname]; The "Almighty initialization Method" is used here.

Copy->_friends = [_friends mutablecopy]; The data structure in the class may not be set in either the initialization method or the "All-in-all initialization method", which needs to be set separately.

The _friends here is the instance variable used in. M.

Or

Copy->_friends = [Nsmutableset alloc]initwithset:_friends copyitems:yes]; If CopyItem is yes, the method sends a copy message to each element in the array. Create a new set with a copy of the good elements.

These two approaches illustrate that objects that comply with the Nscopying protocol are not necessarily implemented in a deep copy manner.

return copy;

}

Copy of the variable version:

Nsmutablecopying's agreement. Only one method is defined:

-(ID) Mutablecopywithzone: (nszone*) zone;

Attention:

Calling the Copy method on a Mutable object returns an instance of another immutable class (Mutablecopy). This is done in order to switch freely between the mutable version and the immutable version.

However, there may be a way to return a Nsmutablearray object as Nsarray to you, and you call the Copy method above to copy it. The result is that the copied object is an immutable array, but it is actually mutable ( runtime characteristics), so this is a problem.

type information can be queried (see 14th) to determine if the instance being copied is mutable.

deep Copy and shallow copy (shallow copy)

Deep copy: When copying the object itself ( pointer ), its underlying data is also copied over.

Shallow copy: Copies only the object itself (pointer).

All collection Classes in the Foundation Framework perform a shallow copy by default,

Summarize:

If you want your custom class to support copy operations, you need to implement the Nscopying protocol.

If a custom object is divided into a mutable version and an immutable version, then both the nscopying and nsmuablecopying protocols are implemented.

When copying objects, you should decide whether to use a shallow copy or a deep copy, in general you should do a shallow copy as much as possible.

If you are writing objects that require a deep copy, consider adding a new method that specifically performs deep copies.

22nd: Understanding the Nscopying Protocol

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.