Shallow copy, deep copy in Objective-c

Source: Internet
Author: User

Shallow copy: Copies only pointers to objects, not the reference object itself. Counter +1, like retain, for example.

Deep copy: Copies the reference object itself. The counter does not change, resulting in a new object

Retain: Always shallow copy. The reference count is added one at a time. Returns whether the object is mutable and consistent with the object being copied.

Copy: The reference count does not change for a deep copy of a mutable object, and for an immutable object is a shallow copy, the reference count is added one at a time. Always returns an immutable object.

Mutablecopy: Always deep copy, reference count does not change. Always returns a Mutable object.

Application: Array type conversion:

1. Immutable objects → conversion of mutable objects [XXX mutablecopy]

2. Variable objects → conversion of immutable objects [XXX copy]

3. Variable object → variable object conversion (different pointer variable pointing to different memory address) [XXX mutablecopy]

4. Pointer duplication between objects of the same type (different pointer variables point to the same memory address) [XXX copy]

When multiple pointers point to the same area of memory, these pointers also have ownership of the memory area. The process of the partition of ownership, when it is necessary to use a shallow copy.

Shallow copy, deep copy in 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.