Objective-C basic notes (2) @ property and @ synthesize
Paste the code in the previous article using @ property and @ synthesize, and then explain the usage and meaning of the two keywords. The Code is as follows:
Person. h file
# Import
@ Interface Person: NSObject {int _ age; // you can access the quilt class. // The system will generate a default int _ no private variable (not accessible to the quilt class )} @ property int age; @ property int no; // write a constructor by yourself-(id) initWithAge :( int) age andNo :( int) no; @ end
Person. m file
# Import "Person. h "@ implementation Person // Xcode 4.5 or above do not need to write the following two sentences (which can be omitted, and the default values are _ age and _ no) // @ synthesize age = _ age; // declared as protected // @ synthesize no = _ no; // The default generation is private-(id) initWithAge :( int) age andNo :( int) no {if (self = [super init]) {_ age = age; _ no = no;} return self;}-(NSString *) description {return [NSString stringWithFormat: @ "age is % I and no is % I", _ age, _ no];} @ end
Main. m file
# Import
# Import "Person. h "int main (int argc, const char * argv []) {@ autoreleasepool {Person * person = [[Person alloc] initWithAge: 15 andNo: 2]; NSLog (@ "age is % I and no is % I", person. age, person. no); [person setNo: 3]; NSLog (@ "no is % I", [person no]); // % @ indicates printing an OC object NSLog (@ "% @", person);} return 0 ;}
Output result:
21:53:15. 406 firstOCProj [826: 47802] age is 15 and no is 2
21:53:15. 407 firstOCProj [826: 47802] no is 3
21:53:15. 408 firstOCProj [826: 47802] age is 15 and no is 3
As you can see, the code above is quite concise. The @ property function is equivalent to the declaration of member variables. @ synthesize is equivalent to the standard implementation of member variables setter and getter.
Note that:
1. You do not need to write the @ synthesize attribute above Xcode4.5 (added by the compiler by default ).
2. This method can be used in combination with the previous method (the old one.
3. If you want to perform special processing on the setter or getter method, you can use the old method (the compiler will not implement it by default ).
4. If no explicit variables are declared, a private member variable is generated by default (not available in the quilt class, as shown in _ no above ).
The above code is simpler:
Person. h
#import
@interface Person : NSObject@property int age;@property int no;@end
Person. m
#import "Person.h"@implementation Person@end
Main. m
#import
#import "Person.h"int main(int argc, const char * argv[]) { @autoreleasepool { Person *person = [Person new]; [person setAge:20]; [person setNo:3]; NSLog(@"age is %i and no is %i", person.age, person.no); } return 0;}
Output result:
22:14:44. 002 firstOCProj [849: 52867] age is 20 and no is 3
Note: My compiler Xcode is more than 4.5, so the @ synthesize attribute in Person. m can be omitted.
I have to lament the OC and Xcode compilers. It is my first time to see such a convenient and useful tool.