We all know that @ property and @ synthesize can automatically generate an access method for a class member variable. However, they may not be very familiar with some properties in property, and some on the Internet are not very correct, it may mislead new users, so I want to introduce the detailed attributes in property in detail.
First, we will introduce the default situation:
Readwrite: This attribute is the default and will automatically generate an accesser for you.
Assign: This attribute is generally used to process basic types, such as int and float. If the declared attribute is of the basic type, assign is the default type. You can skip this attribute.
For assign, its accessorsCodeYes:
@ Property (nonatomic, assign) nsstring * myfield-(nsstring *) myfield {return myfield;}-(void) setmyfield: (nsstring *) newvalue {myfield = newvalue ;}
Atomic: This attribute is available by default to ensureProgramIn the case of multithreading, the compiler will automatically generate some mutex lock code to avoid the problem of non-synchronization of read/write of the variable.
Then let's talk about other situations:
Readonly: Only getter generation does not have the setter Method
Copy: this will automatically generate the clone of the assigned object, which is equivalent to generating a new copy of the object in the memory. As a result, changing the assigned object will not change the member variable you declared.
Retain: Automatically assigns a value to the retain object. The specific implementation is as follows:
@ Property (nonatomic, retain) nsstring * myfield-(nsstring *) myfield {return myfield;}-(void) setmyfield: (nsstring *) newvalue {If (newvalue! = Myfield) {[myfield release]; myfield = [newvalue retain] ;}}
Therefore, you must first determine whether the current myfield is the object assigned with a new value. If you do not need to release yourself, the values and retain will be assigned.
Nonatomic: if you do not need to consider multithreading for this object, add this attribute so that the compiler will generate less mutex lock code, which can improve efficiency.