Shallow copy and deep copy in Objective-c

Source: Internet
Author: User

Shallow copy

A shallow copy is a copy of the memory address, so that the target object pointer and source object point to the same piece of memory space. Such as:

char* str = (char*) malloc (+); char* str2 = str;

A shallow copy is simply a simple copy of the object, allowing several objects to share a piece of memory, and when the memory is destroyed, several pointers to the memory need to be redefined before they can be used, or they will become wild pointers.

A shallow copy inside IOS:

In IOS, using the Retain keyword for reference counting is a more insured, shallow copy. He allows a few pointers to share the same memory space, but also in release due to the existence of the count, will not be easy to destroy the memory, to achieve a more simple use.

Deep copy:

Deep copy refers to the specific contents of the Copy object, and the memory address is self-allocated, after the end of the copy, two objects, although the value is the same, but the memory address is not the same, two objects do not affect each other, non-interference.

The difference between copy and retain:

Copy is to create a new object, retain is to create a pointer, reference the object Count plus one. The Copy property identifies two objects with the same contents, the new object retain count is 1, independent of the old object reference count, and the old object does not change. Copy reduces the object's dependency on the context.

Deep copy in iOS:

iOS provides copy and Mutablecopy methods, as the name implies, copy is copying a Imutable object, and Mutablecopy is copying a mutable object. Here are a few examples to illustrate.
This refers to objects such as NSString, NSNumber, and so on.

NSString *string = @ "dddd"; NSString *stringcopy = [string copy]; Nsmutablestring *stringdcopy = [string mutablecopy]; [Stringmcopy appendstring:@ "!!"];

View memory you can see that the string and stringcopy point to the same piece of memory area (weak reference), and the reference count has not changed. And Stringmcopy is what we call the true meaning of replication, the system allocates a new memory for it, is two separate string content is the same.

Copy construction:

Of course not all objects in iOS support Copy,mutablecopy, classes that follow the Nscopying protocol can send copy messages, and classes that follow the Nsmutablecopying protocol can send mutablecopy messages.

If a copy or mutablecopy is sent without complying with the appeal agreement, an exception will occur. However, the default iOS class does not follow these two protocols. If you want to customize copy then you must obey Nscopying and implement Copywithzone: method, if you want to customize mutablecopy then you must obey Nsmutablecopying, and implement Mutablecopywithzone: Method.

If it is the object we define, then we have to implement nscopying ourselves, nsmutablecopying so that we can invoke copy and Mutablecopy. As an example:

@interface myobj:nsobject<nscopying, nsmutablecopying>{nsmutablestring *_name;    NSString * _IMUTABLESTR;     int _age;} @property (nonatomic, retain) nsmutablestring *name; @property (nonatomic, retain) nsstring *imutablestr; @property ( nonatomic) int age;

Copy Copy construction:

-(ID) Copywithzone: (Nszone *) zone{MyObj *copy = [[self class] allocwithzone:zone] init];    Copy->name = [_name copy];    COPY->IMUTABLESTR = [_imutablestr copy];    Copy->age = age; return copy;}

Mutablecopy Copy Construction:

-(ID) Mutablecopywithzone: (Nszone *) zone{MyObj *copy = nscopyobject (self, 0, zone);    Copy->name = [_name mutablecopy];    Copy->age = age; return copy;}

Http://www.cocoachina.com/ios/20141113/10213.html

Shallow copy and 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.