Deep copy and shallow copy of iOS

Source: Internet
Author: User

At the very beginning, we need to be clear on the basics of how memory is allocated.

Generally divided into stacks, heaps, static variable storage, global variable storage, code area.

The first two people know. Typically, the latter three merges are called Static stores, which store some global variables, static variables, constants, execution code, and so on.

In Objective-c, immutable groups, immutable dictionaries, and some constant strings are all allocated in this area, so let's make this clear.

Therefore, when referring to the depth of the copy, with the Nsarray example, can only say that the memory allocation method is not clear, because the copy operation of an immutable group, it actually returns an object, is not related to the dark copy, because all are processed according to retain. This is the pointer copy that many of the so-called tutorials refer to.

Let's talk about variable and immutable copies, respectively, following the nscopying and nsmutablecopying protocols, which need to be implemented Copywithzone: Methods and Mutablecopywithzone: methods.

In two cases, one is a system container class and one is a custom class.

First, the System container class.

For example, Nsarray, nsdictionary, they have implemented the above two protocols.

For them, the rules are simple, and obj2 = [obj1 copy] Returns is necessarily an immutable object, whether the obj1 is a mutable object or an immutable object. If Obj1 is an immutable object, then they point to the same object, as I mentioned in the previous article.

Obj2 = [Obj1 mutablecopy] Returns is necessarily a mutable object, whether the obj1 is a mutable object or an immutable object. Even if Obj1 is a mutable object, they still point to a different address, which is two objects.

Second, the custom class.

Because Copywithzone: and Mutablecopywithzone: Implemented entirely by themselves, the different implementations of the code determine what the return object is.

In the demo, the element class Copywithzone: There are comments, interested can refer to.

An extreme example, for example, if you return self directly in the Copywithzone: method, then obj2 = [obj1 copy] is equivalent to Obj2 = Obj1, just a assign, without any other action.

The following highlights a shallow copy and a deep copy .

First, as the name implies, whether it is a shallow copy or deep copy, there is a copy in the inside, those who say the shallow copy equivalent to retain, what so-called pointer copy, suggest or not fraught the good.

Here we take Nsmutablearray as an example

Nsmutablearray *element = [Nsmutablearray arraywithobject:@1];

Nsmutablearray *array = [Nsmutablearray arraywithobject:element];

ID mutablecopyarray = [array mutablecopy];

This code is a shallow copy, which copies the container itself, returns a new mutable array, and points to a different memory address.

The inner element remains common, that is, mutablecopyarray[0] also points to element,[mutablecopyarray[0] addobject:***], which affects the result of the array.

ID deepmutablecopyarray = [array test_deepmutablecopy];

This code corresponds to the implementation is a deep copy, first it also copied the container itself, returned a new mutable array, pointing to a different memory address.

Secondly, the internal elements are also copied, that is, Deepmutablecopyarray[0] is a new mutable array, and the original element is two arrays, modify [deepmutablecopyarray[0] addobject:** *] does not affect the result of the array.

Deep copy and shallow copy of 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.