@ 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
- @property int count;
is equivalent to declaring 2 methods in a header file:
C code
- -(int) count;
- -(void) SetCount: (int) Newcount;
2. In the implementation file (. m)
C code
- @synthesize count;
is equivalent to implementing 2 methods in an implementation file (. m).
C code
- -(int) Count
- {
- return count;
- }
- -(void) SetCount: (int) Newcount
- {
- Count = Newcount;
- }
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
- @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
- @property (Nonatomic,retain) test* thetest;
- @property (nonatomic, copy) test* thetest;
Equivalent code:
C code
- -(void) Thetest
- {
- return thetest;
- }
2.
C code
- @property (retain) test* thetest;
- @property (copy) test* thetest;
Equivalent code:
C code
- -(void) Thetest
- {
- [Thetest retain];
- return [Thetest autorelease];
- }
Setter Analysis
1.
C code
- @property (Nonatomic,retain) test* thetest;
- @property (retain) test* thetest;
is equivalent to:
C code
- -(void) Setthetest: (Test *) newthetest {
- if (thetest!= newthetest) {
- [Thetestrelease];
- thetest= [newthetest retain];
- }
- }
2.
C code
- @property (nonatomic,copy) test* thetest;
- @property (copy) test* thetest;
is equivalent to:
C code
- -(void) Setthetest: (Test *) newthetest {
- if (thetest!= newthetest) {
- [Thetest release];
- thetest= [newthetest copy];
- }
- }
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
- -(void) Setstr: (nsstring*) value{
- Str=value;
- }
If it is @property (retain) nsstring*str; The automatic setter content is:
C code
- -(void) Setstr: (nsstring*) v{
- if (v!=str) {
- [STR release];
- Str=[v retain];
- }
- }
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