Copy, mutablecopy, shallow copy, deep copy of objects in Objective-c

Source: Internet
Author: User



The copy of the object is to copy an object as a copy, and he will open up a new memory (heap memory) to store the replica object, just as the file is copied, that is, the source object and the replica object are two different areas of memory. object to have replication function, must implement the <NSCopying> protocol or <NSMutableCopying> protocol, commonly used replicable objects are: NSNumber, NSString, nsmutablestring, Nsarray, Nsmutablearray, Nsdictionary, nsmutabledictionary



Copy: The copy of the resulting object is immutable



Mutablecopy: The copy of the resulting object is mutable






Shallow copy and deep copy



Shallow copy values copy the object itself, the attributes in the object, the contained objects do not replicate



A deep copy copies the object itself, and the object's properties copy a



A class in the foundation that supports replication, by default shallow copy






Custom copies of objects



An object has a copy attribute, which implements the Nscopying,nsmutablecopying protocol to implement the Copywithzone: Method or Mutablecopywithzone: Method of the Protocol.



Shallow copy implementation





[CPP]View Plaincopyprint?
  1. -(ID) Copywithzone: (Nszone *) zone{
  2. Person *person = [[[Self class]allocwithzone:zone]init];
  3. P.name = _name;
  4. P.age = _age;
  5.            return &NBSP;PERSON;&NBSP;&NBSP;
  6. }

implementation of deep copy







[CPP]View Plaincopyprint?
  1. -( void
  2.             person  *person = [[[self class]allocwithzone:zone]init];  
  3. Person.name = [_name copy];
  4. Person.age = [_age copy];
  5.            return &NBSP;PERSON;&NBSP;&NBSP;
  6. }

the relationship between a deep copy and a retain





The relationship between copy, Mutablecopy, and retain



The replicable object in the foundation, when we copy an immutable object, it works fairly well with retain (Cocoa memory optimizer)



When we use Mutablecopy, the copy is mutable regardless of whether the source object is mutable or not.



When we copy a mutable object, the copy is not mutable



Copy, mutablecopy, shallow copy, deep copy of objects 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.