Objective-C shallow copy and deep copy

Source: Internet
Author: User
Shortest copy

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

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

The light copy is only a simple copy of the object, so that several objects share a piece of memory. When the memory is destroyed, several pointers pointing to the memory need to be redefined before it can be used, otherwise it will become a wild pointer.

Shortest copy in IOS:

In iOS, using the retain keyword for reference counting is a more secure and shortest copy. It not only allows several pointers to share the same piece of memory space, but also makes it easier to use the release memory because of the existence of the count.

Deep copy:

Deep copy refers to the specific content of the object to be copied, and the memory address is allocated independently. After the copy is completed, although the two objects have the same storage value, the memory address is different, the two objects do not affect each other and do not interfere with each other.

Differences between copy and retain:

Copy is to create a new object, retain is to create a pointer, reference object count plus one. The copy attribute identifies two objects with the same content. The new object retain count is 1, which is irrelevant to the reference count of the old object. The old object remains unchanged. Copy reduces the context dependency of objects.

Deep copy:

IOS provides the copy and mutablecopy methods. As the name suggests, COPY Copies an imutable object, while mutablecopy copies a mutable object. The following are examples.
This refers to objects such as nsstring and nsnumber.

NSString *string = @”dddd"; NSString *stringCopy = [string copy];NSMutableString *stringDCopy = [string mutableCopy];[stringMCopy appendString:@"!!"];

Check the memory. We can see that string and stringcopy point to the same weak reference, and the reference count has not changed. Stringmcopy is what we call true replication. The system allocates new memory for it, and the content of two independent strings is the same.

Copy structure:

Of course, not all objects in IOS support copy and mutablecopy. classes that comply with the nscopying protocol can send copy messages. classes that comply with the nsmutablecopying protocol can send mutablecopy messages.

If a copy or mutablecopy message is sent without complying with the appeal protocol, an exception occurs. However, the default IOS class does not comply with these two protocols. To customize copy, you must comply with nscopying and implement copywithzone: method. to customize mutablecopy, you must comply with nsmutablecopying and implement mutablecopywithzone: method.

Copy structure:

If the object is defined by us, we need to implement nscopying and nsmutablecopying so that we can call copy and mutablecopy. For 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 structure:

(id)copyWithZone:(NSZone *)zone{    MyObj *copy = [[[self class] allocWithZone :zone] init];    copy->name = [_name copy];    copy->imutableStr = [_imutableStr copy];    copy->age = age;    return copy;}

Copy structure:

- (id)mutableCopyWithZone:(NSZone *)zone{    MyObj *copy = NSCopyObject(self, 0, zone);    copy->name = [_name mutableCopy];    copy->age = age;    return copy;}



Objective-C shallow copy and deep copy

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.