@ Property
Introduction
Objective-C language keyword, paired with @ synthesize.
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:
@ Property int count;
It is equivalent to declaring two methods in the header file:
-(INT) count;
-(Void) setcount :( INT) newcount;
2. Implementation file (. m)
@ Synthesize count;
It is equivalent to implementing two methods in the implementation file (. m.
-(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:
@ Property (nonatomic, retain) uiwindow * window;
There are three types of parameters:
Read/write attributes: (readwrite/readonly)
Setter semantics: (assign/retain/copy)
Atomicity: (Atomicity/nonatomic)
The parameter meanings are as follows: readwrite
Generate the setter \ getter Method
Readonly
Only generate simple getter without setter.
Assign
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 performs the copy operation, which is the same as the retain method.
Nonatomic
Multithreading and Variable Protection are prohibited to improve performance.
Parameter types
The retain and copy parameters are complex. The specific analysis is as follows:
Getter Analysis
1. @ property (nonatomic, retain) test * thetest; @ property (nonatomic, copy) test * thetest;
Equivalent code:
-(Void) thetest
{
Return thetest;
}
2. @ property (retain) test * thetest;
@ Property (copy) test * thetest;
Equivalent code:
-(Void) thetest
{
[Thetest retain];
Return [thetest autorelease];
}
Setter Analysis
1,
@ Property (nonatomic, retain) test * thetest;
@ Property (retain) test * thetest;
It is equivalent:
-(Void) setthetest :( test *) newthetest {
If (thetest! = Newthetest ){
[Thetestrelease];
Thetest = [newthetest retain];
}
}
2. @ property (nonatomic, copy) test * thetest;
@ Property (copy) test * thetest;
It is equivalent:
-(Void) setthetest :( test *) newthetest {
If (thetest! = Newthetest ){
[Thetestrelease];
Thetest = [newthetest copy];
}
}