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.
To put it bluntly, it is the getter setter method in java.
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)
Each parameter has the following meanings:
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.
Assign is usually used for basic types, such as int, bool, and char.
Copy is usually used for NSString, NSNumber, NSArray, and other unchanged types.
This is used by retain NSObject.
The following explains why the setter method of NSString type variables should use copy. directly add the Code:
@ Interface
Person
@ Property
(Nonatomic,
Retain) NSString
* Name;
@ End
Person
* P = [[Person alloc] init];
NSMutableString
* Name = [[NSMutableString
Alloc] initWithString: @ "hello"];
P. name
= Name;
[Name
AppendString :@"
World "];
NSLog (@ "% @",
P. name );//
At this time, p. name has changed to hello world, which is not what we want.
If
@ Interface
Person
@ Property
(Nonatomic,
Copy)
NSString
* Name;
@ End
Person
* P = [[Person alloc] init];
NSMutableString
* Name = [[NSMutableString
Alloc] initWithString: @ "hello"];
P. name
= Name;
[Name
AppendString :@"
World "];
NSLog (@ "% @",
P. name );//
P. name is still hello, which is correct.
The value of the NSString type variable will not change, but the memory address of the variable will change. Copy is used to copy the content and allocate a new memory address. Retain copies the memory address of the input parameter and assigns it to the member variable. Therefore, the difference between copy and retain is that if the input parameter is of the NSMutableString type, the content of the member variable will also change once this parameter changes, this will not happen (because the memory addresses of the two are completely different ). If the input parameter is NSString, the effect of using copy and retain is exactly the same. Therefore, we recommend that you use copy for unchangeable types such as NSString and NSArray.
The above Code comes from cocoachina