Deep copy and light copy for iOS, and ios copy

Source: Internet
Author: User

Deep copy and light copy for iOS, and ios copy

At the beginning, we need to know some basic knowledge about memory allocation methods.

Generally, it can be divided into Stack, heap, static variable storage area, global variable storage area, and code area.

The first two are understandable. The last three merge operations are usually called static storage areas, storing some global variables, static variables, constants, and Execution Code.

In Objective-C, immutable arrays, immutable dictionaries, and some constant strings are all allocated in this region. We need to clarify this point first.

So when we talk about deep copy, the NSArray is used as an example. It is only unclear about the memory allocation method, because the copy operation on an immutable array actually returns an object, it has nothing to do with the depth copy, because it is processed according to retain. This is also the pointer copy mentioned in many so-called tutorials.

 

Next, let's talk about the variable copy and the immutable copy protocols, which follow the NSCopying and NSMutableCopying Protocols respectively. We need to implement the copyWithZone: method and mutableCopyWithZone: method.

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

1. System containers.

For example, NSArray and NSDictionary have implemented the above two protocols.

For them, the rule is simple. obj2 = [obj1 copy] returns an immutable object, whether obj1 is a mutable object or an immutable object. If obj1 is an immutable object, it points to the same object, which I mentioned earlier.

Obj2 = [obj1 mutableCopy] returns a mutable object, no matter whether obj1 is a mutable object or an immutable object. Even if obj1 is a mutable object, they still point to different addresses and are two objects.

Ii. Custom classes.

Because copyWithZone: And mutableCopyWithZone: are fully implemented by themselves, different implementation methods of the Code determine what the returned object is.

CopyWithZone of the Element class in the demo: there are comments. For more information, see.

For example, if you directly return self in the copyWithZone: method, obj2 = [obj1 copy] is equivalent to obj2 = obj1, which is just an assign without any other operations.

 

The following describes the shortest copy and deep copy.

First of all, as the name suggests, there is a copy in both the shortest copy and the deep copy. The shortest copy is equivalent to the retain and what is the so-called pointer copy. We suggest you do not miss your child.

Here we use NSMutableArray as an example.

NSMutableArray * element = [NSMutableArray arrayWithObject: @ 1];

NSMutableArray * array = [NSMutableArray arrayWithObject: element];

 

Id mutableCopyArray = [array mutableCopy];

This code is a shortest copy. It copies the container itself and returns a new variable array pointing to different memory addresses.

The internal elements are still public, that is, mutableCopyArray [0] also points to the element, and [mutableCopyArray [0] addObject: ***] will affect the array results.

 

Id deepMutableCopyArray = [array test_deepMutableCopy];

The implementation of this code is deep copy. First, it also copies the container itself and returns a new variable array pointing to different memory addresses.

Second, the internal elements are also copied, that is, deepMutableCopyArray [0] is a new variable array, and the original element is two arrays. Modify [deepMutableCopyArray [0] addObject: * **] does not affect the results of array.

 

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.