The concept and usage of copy in iOS

Source: Internet
Author: User

Copy&mutablecopy
Use the copy and Mutablecopy methods to create a copy of an object.
Copy
Need to implement Nscopying protocol
Immutable replicas are created (such as NSString, Nsarray, Nsdictionary)

Mutablecopy
Need to implement the Nsmutablecopying protocol first
A mutable copy is created (such as nsmutablestring, Nsmutablearray, Nsmutabledictionary)

The purpose of copy is to create a copy, while modifying the original object and copy without interfering with each other.

Deep copy & Shallow copy
Deep copy
A content copy, where the source object and the copy point to a different two objects.
The source object reference counter is unchanged, and the replica counter is set to 1.

Shallow copy
Pointer copy, source object and copy point to the same object
The object reference counter +1, in fact, is equivalent to doing a retain operation.

Only immutable objects that create immutable replicas (copy) are shallow copies, others are deep copies.

Copywithzone of Custom objects
-(ID) Copywithzone: (Nszone *) zone
{
Person *p = [[[Self class] allocwithzone:zone] init];
P.name = Self.name;
P.age = Self.age;
return p;
}

The concept and use of copy in 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.