Deep copy and shallow copy of iOS development (mutablecopy and copy)

Source: Internet
Author: User

The difference between copy and retain:

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

The Retain property indicates that two objects have the same address (creating a pointer, copy of the pointer) and, of course, the same content, the retain value of this object +1 thatis, retain is a pointer copy, copy is a copy of the content.

of course not all objects in iOS support copy, mutablecopy, classes that obey 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.

iOS provides copy and Mutablecopy methods, as the name implies, copy is copying a Imutable object, and Mutablecopy is copying a mutable object.

Deep copy and shallow copy of iOS development (mutablecopy and copy)

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.