@ 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:
1 int count;
is equivalent to declaring 2 methods in a header file:
1 - (int)count;
2 -(void)setCount:(int)newCount;
2. In the implementation file (. m)
1 @synthesize Count;
is equivalent to implementing 2 methods in an implementation file (. m).
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:
1
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.
1 @property(nonatomic,retain)test* thetest;
2 @property(nonatomic ,copy)test* thetest;
Equivalent code:
1 -(void)thetest
2 {
3 return thetest;
4 }
2.
1 @property(retain)test* thetest;
2 @property(copy)test* thetest;
Equivalent code:
1 -(void)thetest {
2 [thetest retain];
3 return [thetest autorelease];
4 }
Setter Analysis
1.
1 @property(nonatomic,retain)test* thetest;
2 @property(retain)test* thetest;
is equivalent to:
1 -(void)setThetest:(test *)newThetest {
2 if (thetest!= newThetest) {
3 [thetestrelease];
4 thetest= [newThetest retain];
5 }
6 }
2.
1 @property (nonatomic,copy) test* thetest; 2 @property (copy) test* thetest;
is equivalent to:
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:
1 -(void)setStr:(NSString*)value{
2 str=value;
3 }
If it is @property (retain) nsstring*str; The automatic setter content is:
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.
Reference:
http://blog.eddie.com.tw/2010/12/08/property-and-synthesize/
http://www.cocoachina.com/bbs/read.php?tid=7322
http://www.cnblogs.com/pinping/archive/2011/08/03/2126150.html
The classification of the claims in the property chapter of the official OBJECTIVE-C document has a detailed description of the Class 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:
1 -(void)setName:(NSString*)_name{
2 name = _name;
3 }
- retain
declaration in the setter method, you need to do a retain plus 1 for the value set. such as:
1-(void) setName: (NSString *) _ name {
2 // First determine whether it is consistent with the old object, and if it is inconsistent, assign it.
3 // Because if it is an object, performing the code in the if will cause an extreme situation: when the retain of this name is 1, the set operation of this time will cause the instance name to be released in advance, and the assignment cannot be achieved.
4 if (name! = _Name) {
5 [name release];
6 name = [_name retain];
7}
8 }
- Copy
Call the copy method of this instance to set the cloned object. Implement reference retain.
4, the atomic nature of the access method:
- Nonatomic
By default, the setter and getter implemented through synthesized are atomically accessed. When multiple threads are accessed simultaneously, the guaranteed access method is accessed only by one thread, such as:
-
1 [ _internal lock ]; // lock using an object-level lock
2 id result = [ [ value retain ] autorelease ];
3 [ _internal unlock ];
4 return result;
-
However, if Nonatomic is set, access to the property is non-atomic access.
Source:http://wiki.magiche.net/pages/viewpage.action?pageId=1540101
@synthesize Tabbarcontroller=_tabbarcontroller;
The name of a getter and setter that is not the same as the variable name can be defined in the @synthesize to protect the variable from inappropriate access.
The content comes from http://justcoding.iteye.com/blog/1444548 thanks to the ladder dream blogger.
Reprint please indicate the source.
The usage of @property and @synthesize in Objective-c