Deep copy and shallow copy in iOS development

Source: Internet
Author: User

Nutshell:

1, for immutable non-collection objects, copy is a pointer copy, Mutablecopy is a copy of the content

2. Copy,mutablecopy is a content copy for mutable non-collection objects

3, for immutable arrays, dictionaries, sets and other collection class objects, copy is a pointer copy, Mutablecopy is a content copy

4, for variable arrays, dictionaries, sets and other collection class objects, copy,mutablecopy are content copies

However, the contents of the collection object are copied only to the object itself, but the elements inside the object are still copied by the pointer. To replicate the entire collection object, there are two ways to use the collection deep copy:

(1) using the Initwitharray:copyitems: method, set the second parameter to Yes

Nsdictionary shallowcopydict = [[Nsdictionary alloc] initwithdictionary:somedictionary Copyitems:yes];

(2) Archive the Collection Object (archive) and then archive (unarchive):

Nsarray *truedeepcopyarray = [Nskeyedunarchiver unarchiveobjectwithdata:[nskeyedarchiver archivedDataWithRootObject : Oldarray]];

Deep copy and shallow copy in iOS development

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.