Keyword in OC, @ property and @ synthesize pair.
Syntax:
@ Property (parameter 1, parameter 2) variable type variable name
@ Synthesize variable name
Function:
Let the compiler automatically compile a method declaration with the same name as the data member, which saves the need for declarative read/write methods.
For example:
// @ Property nsinteger number in the header file; // @ synthesize number in the implementation file;
Equivalent:
// In the header file-(void) setnumber :( nsinteger) newnumber;-(nsinteger) number; // In the implementation file-(void) setnumber :( nsinteger) newnumber {number = newnumber;}-(nsinteger) number {return number ;}
Obviously, using @ property and @ synthesize reduces the workload.
The following parameters are provided:
Nonatomic: atomicity, variable protection, multithreading prohibited, and performance improvement
Atomic: opposite to nonatomic
Retain: The old value of the release parameter in the setter method.
Copy: the setter Method for copy operations, the same as retain
Assign: the setter method directly assigns values without retain operations.
Readonly: only generate simple getter and do not claim the setter Method
Readwrite: generate the setter and getter methods.
Atomic means that the setter/getter function is a primitive operation. If multiple threads call setter at the same time, there will be no execution of setter by another thread before executing all the setter statements, which is equivalent to adding a lock at the beginning and end of the function.
Nonatomic does not guarantee primitive operations of setter/getter, so you may get incomplete things. For example, if you use nonatomic to change two member variables in the setter function, Getter may obtain the state when only one of the variables is changed, which may cause problems. If multithreading is not required, nonatomic is enough. In addition, since the lock operation is not involved, it is faster to execute.
Example:
1. Retain
@ Property (nonatomic, retain) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass retain] ;}}-(class *) Aclass {return Aclass ;}
2. Retain
@ Property (retain) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass retain] ;}}-(class *) Aclass {[Aclass retain]; return [Aclass autorelock];}
3. Copy
@ Property (nonatomic, copy) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass copy] ;}}-(class *) Aclass {return Aclass ;}
4. Copy
@ Property (copy) class * Aclass; @ synthesize Aclass; // equivalent to:-(void) setaclass :( class *) _ Aclass {If (Aclass! = _ Aclass) {[Aclass release]; Aclass = [_ Aclass copy] ;}}-(class *) Aclass {[Aclass retain]; return [Aclass autorelease];}