Deep copy and shallow copy of IOS

Source: Internet
Author: User

In general, there are two broad categories: collection class and non-collection class

Concept: pointer copy commonly known as pointer Copy, object copy is commonly known as content copy;

1, non-collection of deep-type copies such as: NSString, nsnumber, etc.;

The following print is the address result:

It is obvious that Stringh and Stringw address, and STRINGQ address is not the same, the STRINGW is done is a shallow copy (pointer copy), and Stringq is a deep copy (content copy);

Again such as:

The following print is the address result:

Four addresses are all different, and as long as copy, can not be modified; The address is not the same as the complete is a deep copy (content copy);

2, sets of the depth of the copy of the class: such as Nsarray, nsdictionary, etc.;

Https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Collections/Articles/Copying.html

The copy of the collection class and the amazing similarity of the non-collection are just a little bit different; in fact, the so-called deep copy is a bit controversial, but enough for us to use, to really realize deep copy or use the Apple official API mentioned method:

Nsarray *deepcopyarray=[[nsarray alloc] Initwitharray:somearray Copyitems:yes];

OR archive:

nsarray* Truedeepcopyarray = [Nskeyedunarchiver unarchiveobjectwithdata:[nskeyedarchiver archivedDataWithRootObject : Oldarray]];

Compiler has been a problem, crash all, first write to this, the level of limited inevitably have flaws, welcome correction!

Deep copy and shallow copy of IOS

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.