The usage of @property and @synthesize in Objective-c

Source: Internet
Author: User

@ stands for "Objective-c" logo that proves you are using objective-c language

Objective-c language keywords, @property paired with @synthesize.

function: Let the compiler automatically write a method declaration with the same name as the data member, omitting the declaration of the read-write method.

Such as:

1. In the header file:

C code
    1. @property int count;

is equivalent to declaring 2 methods in a header file:

C code
    1. -(int) count;
    2. -(void) SetCount: (int) Newcount;

2. In the implementation file (. m)

C code
    1. @synthesize count;

is equivalent to implementing 2 methods in an implementation file (. m).

C code
    1. -(int) Count
    2. {
    3. return count;
    4. }
    5. -(void) SetCount: (int) Newcount
    6. {
    7. Count = Newcount;
    8. }

The equivalent function section above is automatically filled by the compiler to help the developer, simplifying the coding input effort.

Format:

The syntax for declaring a property is: @property (parameter 1, parameter 2) type name;

Such as:

C code
    1. @property (Nonatomic,retain) UIWindow *window;

The parameters are divided into three main categories:

Read-Write properties: (readwrite/readonly)

Setter semantics: (assign/retain/copy)

Atomicity: (atomicity/nonatomic)

Each parameter has the following meanings:

ReadWrite: Generating Setter\getter method

ReadOnly: Only generates simple getter, no setter.

Assign: Default type, setter method directly assigns value without retain operation

Retain:setter method to release the old value of the parameter, and then retain the new value.

Copy:setter method copy operation, same as retain

Nonatomic: Prohibit multi-threading, variable protection, improve performance

Parameter type

The more complex parameters are retain and copy, the specific analysis is as follows:

Getter Analysis

1.

C code
    1. @property (Nonatomic,retain) test* thetest;
    2. @property (nonatomic, copy) test* thetest;

Equivalent code:

C code
    1. -(void) Thetest
    2. {
    3.   return thetest;
    4. }

2.

C code
    1. @property (retain) test* thetest;
    2. @property (copy) test* thetest;

Equivalent code:

C code
    1. -(void) Thetest
    2. {
    3. [Thetest retain];
    4. return [Thetest autorelease];
    5. }

Setter Analysis

1.

C code
    1. @property (Nonatomic,retain) test* thetest;
    2. @property (retain) test* thetest;

is equivalent to:

C code
    1. -(void) Setthetest: (Test *) newthetest {
    2. if (thetest!= newthetest) {
    3. [Thetestrelease];
    4. thetest= [newthetest retain];
    5. }
    6. }

2.

C code
    1. @property (nonatomic,copy) test* thetest;
    2. @property (copy) test* thetest;

is equivalent to:

C code
    1. -(void) Setthetest: (Test *) newthetest {
    2. if (thetest!= newthetest) {
    3. [Thetest release];
    4. thetest= [newthetest copy];
    5. }
    6. }

Nonatomic

If you use multi-threaded, there will be two threads waiting for each other to cause a deadlock (specifically, you can search the thread of attention to understand). In the absence of (nonatomic), the default (atomic) prevents such threads from being mutually exclusive, but consumes a certain amount of resources. So if it's not a multi-threaded program, hit (nonatomic)

Retain

Code description

If only @property nsstring*str; The setter code generated automatically by @synthesize is:

C code
    1. -(void) Setstr: (nsstring*) value{
    2. Str=value;
    3. }

If it is @property (retain) nsstring*str; The automatic setter content is:

C code
    1. -(void) Setstr: (nsstring*) v{
    2. if (v!=str) {
    3. [STR release];
    4. Str=[v retain];
    5. }
    6. }

Owner Property

Let's take a look at the attributes that relate to ownership, the correspondence between the keywords.

Property value keyword Ownership

Strong __strong Yes
Weak __weak No
unsafe_unretained __unsafe_unretained No
Copy __strong Yes
Assign __unsafe_unretained No
Retain __strong Yes

Strong

The property value corresponds to the __strong keyword, that is, the variable declared by the attribute becomes the holder of the object.

Weak

The attribute corresponds to the __weak keyword, which is consistent with the variable defined by the __weak, that the variable declared by the property will have no ownership of the object, and that the object will be automatically assigned nil after the object is discarded.

Also, delegate and Outlet should be declared with the weak attribute. Also, the weak property is not available if the previous version of IOS 5 that was introduced earlier has no __weak keyword. In this case we use unsafe_unretained.

unsafe_unretained

A variable equivalent to the __unsafe_unretaind keyword declaration, as described above, used by systems prior to IOS 5 in place of weak.

Copy

The difference from strong is that declaring a variable is the holder of the copied object.

Assign

The general scalar varible is declared with this attribute, for example, int, BOOL.

Retain

This property is consistent with strong; it's just more readable.

The usage of @property and @synthesize in Objective-c

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.