Explanation of all attributes of @ property in Objective-C

Source: Internet
Author: User

1, assign:

Simple assignment without changing the index count

Assume that you allocated a piece of memory with malloc and assigned its address to pointer a. Then you want pointer B to share the block of memory, so you assign a value to assign) b. At this time, a and B point to the same memory. Can a directly release this memory when a no longer needs it? The answer is no, because a does not know whether B is still using this memory. If a is released, B will cause the program to crash when using this memory.

 

Application scenarios:

Basic data types (such as NSInteger and CGFloat) and C (int, float, double, char, etc)

Applicable to simple data types

2, retain:

Corresponding to strong, the reference count, retain +, and release-are used. When the reference count is 0, dealloc is called and the memory is released.

 

3. copy:

When used for non-shared memory, each pointer has its own memory space

 

4, atomic // default attribute

/

 

5, nonatomic

 

 

Conclusion: atomic and nonatomic are used to determine whether the getter and setter generated by the compiler are atomic operations. In a multi-threaded environment, atomic operations are necessary; otherwise, errors may occur. When atomic is added, the setter function will become as follows:

 (property !==

 

6, strong: // The default attribute in ARC, which is equal to the retain in non-ARC.

Corresponds to retain,

Application scenarios:

The strong attribute is used in ARC.

@property (strong,nonatomic) ViewController *viewController;

 

7. weak:

Corresponds to assign,

Application scenarios:

IBOutlets, for example, a subclass of UIViewController, that is, a general control.

@property (weak, nonatomic) IBOutlet UIButton *myButton;

 

The difference between strong and weak is as follows:

Prerequisites:

We compare the objects with strong or weak to a kite. The kite wants to break away from the limitations of the line and fly freely. If there is a line at this time, then the kite cannot break away.

 

Process Analysis

 

 

8, readonly

Only get method, no set Method

9, readwrite // default attribute

Get/set methods available

10, unsafe_unretauined

It is used in the ARC compiling environment, and is similar to assign in this environment. It only tells ARC how to correctly call retain and release declared as the unsafe_unretauined variable

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.