@ Indicates the "objective-c" flag, proving that you are using the objective-C Language
Objective-C language keyword, @ property and @ synthesize pair.
Function:Let the compiler automatically write a method declaration with the same name as the data member to save the declaration of the read/write method.
For example:
1. In the header file:
C code
- @ Property int count;
It is equivalent to declaring two methods in the header file:
C code
- -(INT) count;
- -(Void) setcount :( INT) newcount;
2. Implementation file (. m)
C code
- @ Synthesize count;
It is equivalent to implementing two methods in the implementation file (. m.
C code
- -(INT) count
- {
- Return count;
- }
- -(Void) setcount :( INT) newcount
- {
- Count = newcount;
- }
The above equivalent functions are automatically filled by the compiler to help developers, simplifying the coding input workload.
Format:
The syntax for declaring property is: @ property (parameter 1, parameter 2) type name;
For example:
C code
- @ Property (nonatomic, retain) uiwindow * window;
There are three types of parameters:
Read/write attributes: (readwrite/readonly)
Setter semantics: (assign/retain/copy)
Atomicity: (Atomicity/nonatomic)
Each parameter has the following meanings:
Readwrite: generate the setter \ getter Method
Readonly: generate only simple getter without setter.
Assign: the default type. The setter method directly assigns values without retain operations.
Retain: the setter method performs the release old value on the parameter, and then retain the new value.
Copy: the setter Method for copy operations, the same as retain
Nonatomic: multi-thread prohibited, variable protection, and performance improvement
Parameter type
The retain and copy parameters are complex. 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;
It is equivalent:
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;
It is equivalent:
C code
- -(Void) setthetest :( test *) newthetest {
- If (thetest! = Newthetest ){
- [Thetest release];
- Thetest = [newthetest copy];
- }
- }
Nonatomic
If multiple threads are used, two threads may wait for each other to lock up (you can find out the precautions for deprecation ). If there is no (nonatomic), that is, the default (atomic) will prevent such threads from being mutually exclusive, but will consume a certain amount of resources. Therefore, if it is not a multi-threaded program, simply add (nonatomic ).
Retain
Code Description
If it is only @ property nsstring * STR; then the setter code automatically generated by @ synthesize is:
C code
- -(Void) setstr :( nsstring *) value {
- STR = value;
- }
If it is @ property (retain) nsstring * STR; then the automatic setter content is:
C code
- -(Void) setstr :( nsstring *) V {
- If (V! = Str ){
- [STR release];
- STR = [V retain];
- }
- }
Owner attributes
Let's take a look at the mappings between attributes and keywords related to ownership.
Attribute Value keyword ownership
Strong |
_ Strong |
Yes |
Weak |
_ Weak |
None |
Unsafe_unretained |
_ Unsafe_unretained |
None |
Copy |
_ Strong |
Yes |
Assign |
_ Unsafe_unretained |
None |
Retain |
_ Strong |
Yes |
Strong
The _ strong keyword corresponding to the attribute, that is, the variable declared by this attribute will become the owner of the object.
Weak
This attribute corresponds to the _ weak keyword, which is consistent with the variable defined by _ weak. The variables declared by this attribute do not have the ownership of the object, and when the object is discarded, the object is automatically assigned a value of nil.
In addition, delegate and outlet should be declared using the weak attribute. At the same time, for example, the previous version of IOS 5 does not have the _ weak keyword, so the weak attribute cannot be used. In this case, we use unsafe_unretained.
Unsafe_unretained
It is equivalent to the variable declared by the _ unsafe_unretaind keyword. As described above, the system before iOS 5 uses this attribute to replace weak.
Copy
The difference with strong is that the declaration variable is the holder of the copy object.
Assign
Scalar varible is declared with this attribute, such as int and bool.
Retain
This attribute is consistent with strong, but it is more readable.
Refer:
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
Declared category
In the official objective-C document, the property section describes the class property in detail.
@ Property the declaration list has been classified into the following categories:
1,Access Method for declaring attributes:
- Getter = gettername
- Setter = settername
Declares the access attribute settings and method names.
2,Permission for declaring attribute write operations:
- Readwrite
Declare this attribute as a read/write attribute, that is, you can access the setting method (setter), or you can access the getter method (getter), which is mutually exclusive with readonly.
- Readonly
Declare this attribute as a read-only attribute and only access the getter corresponding to this attribute. It is mutually exclusive with readwrite.
3,Implementation of declarative write methods:
- Assign
The setter method uses direct value assignment to set values. For example:
C code
- -(Void) setname :( nsstring *) _ name {
- Name = _ name;
- }
- Retain
Declaration in the setter method, you need to perform the retain plus 1 operation on the set value. For example:
C code
- -(Void) setname :( nsstring *) _ name {
- // First, determine whether it is consistent with the old object. If it is inconsistent, assign a value.
- // If it is an object, executing the code in if will cause an extreme situation: When the retain of this name is 1, this set operation allows the Instance name to be released in advance without assigning values.
- If (name! = _ Name ){
- [Name Release];
- Name = [_ name retain];
- }
- }
- Copy
Call the copy method of this instance to set the cloned object. Refer to retain for implementation.
4,Atomicity of access methods:
Source: http://wiki.magiche.net/pages/viewpage.action? Pageid = 1540101
@ Synthesize tabbarcontroller = _ tabbarcontroller;
@ Synthesize defines the names of getter and setter, which are different from the variable names, so as to protect the variables from improper access.
@ Property and @ synthesize usage in objective-C