Deep copy and light copy in oC

Source: Internet
Author: User

Shallow shallow copy deep copy

1. generate a new object with the same content as the source object


2. The source object and the new object use different memory regions.


3. You must use the nscopying or nsmutablecopying protocol to use the replication function.


4. Basic data types such as nsstring and nsnumber in fundation all implement nscopying


5. The reference count of the new object is 1.


6. Differences between copy and mutablecopy

Copy returns an immutable object (including a mutable object)

? Mutablecopy returns a mutable object


7. The shortest copy only copies the object itself, but does not copy other objects referenced by the object.


8. In addition to copying the object itself, deep copy also copies other objects referenced by the object.


9. classes that support copying in the foundation framework. The default value is light copy.



Retain: Always light replication. Add one to each reference count. Whether the returned object is changeable is consistent with the Copied object.


Copy: For deep replication of mutable objects, the reference count does not change; for non-mutable objects, the reference count does not change,

Add one to each reference count. Always Returns an unchangeable object.


Mutablecopy: It is always a deep copy, and the reference count remains unchanged. Always Returns a mutable object.


Immutable object: The value changes, and the first address of the Memory changes accordingly.

Variable object: The first address of the memory does not change regardless of whether the value is changed.

Reference count:To give users a clear idea of the number of owners of the object (that is, the number of pointers pointing to the same memory address ).


Recently, a good friend asked me when to use deep replication? So I will share some of my summary with you, hoping to help you better understand the depth of replication!


Let's take a look at the conversion of the array type below.

1. immutable object → variable object conversion:

Nsarray * array1 = [nsarray arraywithobjects: @ "A", @ "B", @ "C", @ "D", nil];

Nsmutablearray * str2 = [array1 mutablecopy];

2. Variable object → non-variable object conversion:

Nsmutablearray * array2 = [nsmutablearray arraywithobjects: @ "AA", @ "BB", @ "cc", @ "DD", nil];

Nsarray * array1 = [array2 copy];

3. Variable object → variable object Conversion(Different pointer variables point to different memory addresses):

Nsmutablearray * array1 = [nsmutablearray arraywithobjects: @ "A", @ "B", @ "C", @ "D", nil];

Nsmutablearray * str2 = [array1 mutablecopy];

Using the two examples above, we can easily convert an object between mutable and immutable, and here we do not need to consider the memory usage principle (that is, the reference count problem ). Yes, this is the charm of deep copy.

4. pointer replication between objects of the same type (different pointer variables point to the same memory address ):


 Nsmutablestring * str1 = [nsmutablestring stringwithstring: @ "two day"];

Nsmutablestring * str2 = [str1 retain];

[Str1 release];


Nsarray * array1 = [nsarray arraywithobjects: @ "A", @ "B", @ "C", @ "D", nil];

Nsarray * str2 = [array1 copy];

[Array1 release];


In general, if multiple pointers direct to the same memory area at the same time, these pointers also have ownership of the memory area.. In this case, a shortest copy is required.

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: 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.