Objective-c Road "18-Copy Object"

Source: Internet
Author: User

Shimen the main contribution, respect the work of the author, please do not reprint.

If the article is helpful to you, you are welcome to donate to the author, support Shimen, and donate any amount. Heavy on the mind ^_^

I want to donate: Click Donate

Cocos2d-x source code Download: Dot I teleport


Assigning a variable to another object only creates a reference to the object.

Similar to Java, an assignment is a reference.

The reference to C + + or C pointers to understand is easy. The assignment here is actually the pointer address assignment.


The Foundation class implements a method called copy and mutablecopy that enables you to create a copy of an object using these methods.

Complete this task by implementing a method that conforms to the <NSCopying> protocol (for making a copy).


Note that generating a mutable copy of an object does not require that the copied object itself be mutable. , you can create immutable copies of Mutable objects.


Shallow copy and deep copy:

Mutablecopy method when copying an array. The new Array object is allocated space in memory. and a single element is copied into the new array.

Then copying each element in the original array to a new location means: Copy the reference from only one array element to another array element.

The result is that the elements in the two array all point to the same string in memory.

To create a completely different copy of each element in an array, you need to run a so-called deep copy.


Implementation <NSCopying> Agreement:

Attention. Suppose you want to differentiate between a mutable copy and an immutable copy.

It is also necessary to implement Mutablecopywithzone based on <NSMutableCoping> protocol: methods.

Assume that two methods are implemented. Then Copywithzone: The immutable copy should be returned.

Instead, Mutablecopywithzone: A mutable copy is returned.


Property does not have a mutablecopy attribute.

Even a mutable instance variable. It is also used with the copy feature, as the method Copywithzone: Operating performance.

So. By protocol, an immutable copy of an object is produced.

Objective-c Road "18-Copy Object"

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.