Differences between assign retain and copy

Source: Internet
Author: User

Assign

For basic data types (nsinteger, cgfloat) and C data types (INT, float, double, char), etc. This mark indicates that the seter directly enters? Row assignment, which is also the default value. In? Should garbage collection be used? In the application, if you want? An Attribute enables? When assign is used and the class complies with the nscopying agreement, you need to specify this mark ,? Instead of simply making? Use the default value. Otherwise, you will get? A compilation warning. This again tells the compiler that you really need to assign values, even if it is copyable? Bay.

RetainFor other nsobject and its? What are the parameters of subclass? The old value of row release. If the new value of retain is specified, the system will wake up when the value is assigned? The retain message of the input value. This attribute can only be? Used for objective-C object type ,? But no? Used for Core

Foundation object. (The reason is obvious. does retain add an object? Count ,? The basic data type or core Foundation object is not cited? Count-Translator's note ).

Note: when an object is added to an array? Will the reference of the object be added by counting? Usage + 1.

Copy

It indicates to nsstring That? Use transfer? Input value? Copy? Bay. Copy? Bay? Work by copy? Method execution? Row, this property is only for those instances? The object type of the nscopying protocol is valid. Deeper? For more information, see "copy .? Copy and retain:

Is copy actually created? ? An identical object ,? And retain is not :? 1 .? For example? An nsstring object with the address 0 × 1111, with the content @ "STR". Copy it to another? After an nsstring, the address is 0 × 2222, with the same content .? 2. The new object retain is 1, and the old object does not change retain to another? After an nsstring, the address is the same (create? Li? A pointer, copy the pointer? (Bay), the content is of course the same. The retain value of this object is + 1 .? Conclusion: Is retain a pointer copy? Bei, is copy content copied? Bay .? Assign and retain :? 1. I have been in contact with C, so suppose you? Allocated with malloc? A piece of memory, and assigned its address to pointer A. Then you want pointer B to share the memory, so you? Assign a to (assign) B. In this case, a and B point to the same? One piece of memory. Can I directly release this piece of memory when a no longer needs it? The answer is no, because a does not know whether B is still working? Use this memory. If a is released, is B working? Using this memory will cause program crash loss .? 2. I learned about the assign problem in 1. How can I solve it? The simplest? One? The method is to make? Reference? Reference counting? Which of the following examples? What is the memory we set? An introduction? Count. When the memory is allocated and assigned to a, why? The count is 1. When a is assigned to B? Increase the count to 2. If a does not enable this function? With this memory, it only needs to introduce? Minus 1 by count, indicating? ? This memory is no longer available. B No longer enable? When using this memory? Minus 1 by count. When? When the count is changed to 0, does the memory no longer be cited by any pointer? The system can release it directly. Conclusion: upper? The difference between assign and retain is actually two points. Assign is a direct value assignment, from? But it may cause problems in 1. When the data is int, float, and other original data? When the type is generated, can you set? Use assign. Retain, as described in 2, enables? Why? Is it caused by counting and retain? Add 1 to the count and cause the quotation with release? Minus 1 by count. When? When the count is 0, is the dealloc function called? Memory is recycled.

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.