When to use retain and copy

Source: Internet
Author: User

Copy: Create an object with an index count of 1, then release the old object retain: Release the old object, and assign the value of the old object to the input object, then increase the index count of the input object to 1. What does the above mean? Copy actually creates the same object, but retain is not: for example, if an NSArray object has an address of 11111 and the content is xxCopy to another NSArray object, the address is 22222 and the content is the same, the new object retain is 1, and the old object does not change retain to another NSArray, the address is the same (create a pointer, copy pointer), and the content is of course the same, the retain value of this object is + 1, that is, retain is a pointer copy, and copy is a content copy. Under what conditions is assign used, retain used, and copy used? Assign: assign, instead of retain or copy, should be used for non-having relations. Retain or copy: when the class has mutable subclass, you should use copy instead of retain. For example: NSCharacterSet, NSDictionary, NSData, NSCharacterSet, NSIndexSet, and NSString.

Related Article

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.