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

Source: Internet
Author: User

Basic concepts and usage of deep-shade copies

Basic concepts

  Shallow copy paper Copy the object itself, the object's properties, the contained object does not copy

A deep copy duplicates both the object itself and the object's properties.

The class that supports replication in the Foundation framework is a shallow copy by default.

Copy and Mutabelcopy

    1. An object can create a copy of an object using the copy and Mutabelcopy methods
    2. Copy-you need to implement the Nscoppying protocol first, creating immutable replicas (such as NSString, Nsarray, Nsdictionary)
    3. Mutabelcopy-You need to implement the Nsmutabelcopying protocol first, creating a mutable copy (such as nsmutabelstring, Nsmutabelarray, Nsmutabeldictionary)
    4. Deep copy: A copy of the content, 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
    5. Shallow copy: pointer copy, source object and copy point to the same object. Object reference counter +1 is actually equivalent to doing a retain operation
    6. Only immutable objects Create immutable replicas (copy) is shallow copy, others are deep copy

Example: A custom copy of an object

The object has a copy attribute, which implements the Nscopying, nsmutabelcopying protocol to implement the copywithzone of the Protocol: Methods and Mutabelcopywithzone: Methods

Code:

Different implementations of the shades of a copy; code examples

The difference between a Objective-c object's dark copy is that you have different implementations of Copywithzone

A summary of the depth of the copy and the retain

The relationship between copy, Mutabelcopy, and retain

    • Foundation replicable object, when we copy an immutable object, his role is equivalent to retain (Cocoa do memory optimization)
    • When we use Mutabelcopy, the copy is mutable regardless of the source master, and it implements a true copy
    • When we copy a mutable object, the replica object is immutable, and it also implements a true copy

Some common replication cases for Foundation

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.