What are the differences between keywords such as _ weak and _ strong and weak and strong?

Source: Internet
Author: User

We recommend that you do not use arc on ios4 devices...

Strong and weak are used to modify attributes.
Strong is used to modify attributes of strong references;
@ Property (strong) someclass * aobject;
Corresponds to the original
@ Property (retain) someclass * aobject; and @ property (copy) someclass * aobject;

Weak is used to modify attributes of weak references;
@ Property (weak) someclass * aobject;
Corresponds to the original
@ Property (assign) someclass * aobject;

_ Weak, _ strong is used to modify the variable, and _ unsafe_unretained and _ autoreleasing are used to modify the variable.
_ Strong is the default keyword.
_ Weak declares a weak reference that can be automatically nil.
_ Unsafe_unretained declares a weak application, but it will not be automatically nil. That is to say, if the memory area to which it is directed is released, this pointer is a wild pointer.
_ Autoreleasing is used to modify a function parameter, which is automatically released when the function returns.

Additional reading:
Beginning arc in iOS 5 tutorial Part 1: http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1

Arc Introduction: http://blog.csdn.net/nicktang/article/details/6887569

 

 

 

Owner attributes

 

Let's take a look at the mappings between attributes and keywords related to ownership.

Attribute Value Keywords Ownership
Strong _ Strong Yes
Weak _ Weak None
Unsafe_unretained _ Unsafe_unretained None
Copy _ Strong Yes
Assign _ Unsafe_unretained None
Retain _ Strong Yes
Strong

The _ strong keyword corresponding to the attribute, that is, the variable declared by this attribute will become the owner of the object.

Weak

This attribute corresponds to the _ weak keyword, which is consistent with the variable defined by _ weak. The variables declared by this attribute do not have the ownership of the object, and when the object is discarded, the object is automatically assigned a value of nil.

In addition, delegate and outlet should be declared using the weak attribute. At the same time, for example, the previous version of IOS 5 does not have the _ weak keyword, so the weak attribute cannot be used. In this case, we use unsafe_unretained.

Unsafe_unretained

It is equivalent to the variable declared by the _ unsafe_unretaind keyword. As described above, the system before iOS 5 uses this attribute to replace weak.

Copy

The difference with strong is that the declaration variable is the holder of the copy object.

Assign

Scalar varible is declared with this attribute, such as int and bool.

Retain

This attribute is consistent with strong, but it is more readable.

 

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.