@ Property usage

Source: Internet
Author: User

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

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.