@ 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.
Classification of claims
The property chapter in the official OBJECTIVE-C document has a detailed description of the property.
The list of claims in the @property has been categorized into the following types:
1, declare the access method for the property:
- Getter=gettername
- Setter=settername
Declares the setting of the Access property and gets the method name.
2, declare attribute Write permission:
- ReadWrite
Declares that this property is read-write, that is, you can access the Set method (setter), or you can access the Get method (getter), which is mutually exclusive with ReadOnly.
- ReadOnly
Declares that this property is read-only and can only access the Fetch method (getter) corresponding to this property, which is mutually exclusive with ReadWrite.
3, declare the implementation of the method:
- Assign
declaration in setter methods, the use of direct assignment to achieve the value-setting operation. Such as:
C code
- -(void) SetName: (nsstring*) _name{
- name = _name;
- }
- Retain
declaration in the setter method, you need to set the value of the Retain plus 1 operation. Such as:
C code
- -(void) SetName: (nsstring*) _name{
- //First determine if it is consistent with the old object, and if it is inconsistent, assign the value.
- //Because if it is an object, the code in the if will cause an extreme situation: when the name of retain is 1 o'clock, this set operation causes the instance name to be released early, and the assignment is not achieved.
- if (name! = _name) {
- [Name release];
- name = [_name retain];
- }
- }
- Copy
Call the copy method of this instance to set the cloned object. Implement reference retain.
4, the atomic nature of the access method:
The usage of @property and @synthesize in Objective-c