One, @property @synthesize keywords
Note: These two keywords are compiler features that allow Xcode to automatically generate The declarations and implementations of getter and setter.
(i) @property keywords
@property keyword can automatically generate a declaration of the setter and getter method for a member variable
@property int age;
When the compilation encounters this line, it is automatically extended to the following two sentences:
-(void) Setage: (int) age;
-(int) age;
(ii) @synthesize keywords
@synthesize keyword helps generate the implementation of the setter and getter methods for member variables.
Syntax:@synthesize age=_age;
Equivalent to the following code:
- (void)setage: (int) Age
{
_age=age;
}
-(int) age
{
Return _age;
}
(iii) Use and use of keywords note
The declarations section of the class:
The implementation part of the class:
Test procedure:
In the new version:
The declarations section of the class:
The implementation part of the class:
Test procedure:
(1) in old-fashioned code,@property can only be written in @interface @end ,@synthesize can only be written in @implementation @end , since after Xcode 4.4 ,@property @property and @synthesize functions.
(2) @property int age;This sentence is complete.31 ) generate _age get and set" Generate _age member variable set and get3 "Create a _age
Note: The member variables generated in this way are private .
(3) You can add int _age to {}; The declaration shown is _age to protected .
(4) Principle:get and set methods are the same as variables, if you define yourself, then use what you have defined, if not defined, then automatically generate a.
(5) Manual implementation:
1) If the Set method is implemented manually , then the compiler generates only the get methods and member variables;
2) If the Get method is implemented manually , then the compiler generates only the set method and member variable;
3) If both the set and get methods are implemented manually, the compiler will not generate member variables.
Second, Id
An ID is a type, universal pointer that can point to \ manipulate any object.
Note: In the definition of ID, the * number is already wrapped . The Id pointer can only point to an OS object.
definition of ID type
Typedef struct OBJC object{
Class Isa;
} *id;
Limitations: Call a nonexistent method, the compiler will immediately error.
OC language @property @synthesize and ID