The difference between deep copy, shallow copy, and retain in iOS

Source: Internet
Author: User

Shallow copy: A shallow copy is a copy of the pointer to the object, so that the pointer points to the same memory address, "Object always has only one", shallow copy makes the object reference counter +1. The code is as follows:

You can see that the pointer to the immutable string points to the same address and does not re-open the memory.

Deep copy: A deep copy is a copy of the object, preserving the value of the original object and opening up a new memory address.

In the code, declare a mutable array, add a copy-decorated property for the Wxdobject class, point the pointer to a mutable array after the implementation of the class, and print the address where the two are found to have changed. So using copy to modify a Mutable object will make a deep copy.

Retain modification: The modification of the above array is changed to retain, the object references counter +1, points to the same address, and does not re-open the memory address.

Summarize:

Retain: Reference counter +1, point to the same address. The result is just like a shallow copy, which simply makes the object's reference counter increment by 1.

Copy: For immutable objects equivalent to retain, pointer Copy, reference counter +1. Performs a deep copy of a mutable object, equivalent to a mutablecopy, content copy.

Mutablecopy: Deep Copy re-creates a new object, but retains the value of the original object. Content copy.

The difference between deep copy, shallow copy, and retain in iOS

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.