When you define a series of variables, you need to write a lot of getter and setter methods, and their form is similar, so Xcode provides the @property and @synthesize properties, @property used in the. h header file as a declaration, @ Synthesize is used in. m files for implementation.
As below, create a project based on "Command line Tool", called "Property", and create a new student class,
The traditional notation is:
Student.h
[CPP]View Plaincopy
- //
- //Student.h
- // Property
- //
- //Created by rio.king on 13-8-25.
- //Copyright (c) 2013 rio.king. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Student:nsobject
- {
- int Age ;
- int No;
- }
- //age getter and setter method declarations
- -(int) age;
- -(void) setage: (int) newage;
- //no getter and setter method declarations
- -(int) No;
- -(void) Setno: (int) Newno;
- @end
Student.m
[CPP]View Plaincopy
- //
- //STUDENT.M
- // Property
- //
- //Created by rio.king on 13-8-25.
- //Copyright (c) 2013 rio.king. All rights reserved.
- //
- #import "Student.h"
- @implementation Student
- implementation of getter and setter method for//age
- -(int) age
- {
- return Age ;
- }
- -(void) setage: (int) newage
- {
- age = NewAge;
- }
- implementation of getter and setter method for//no
- -(int) No
- {
- return No;
- }
- -(void) Setno: (int) Newno
- {
- no = Newno;
- }
- @end
Main.m
[CPP]View Plaincopy
- //
- //MAIN.M
- // Property
- //
- //Created by rio.king on 13-8-25.
- //Copyright (c) 2013 rio.king. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Student.h"
- int Main (int argc, const char * argv[])
- {
- @autoreleasepool {
- //Insert code here ...
- Student *stu = [[Student alloc] init];
- Stu.age = 100; //This sentence is equivalent to setter method
- NSLog (@"Age is%i", stu.age); //Here the Stu.age equivalent getter method
- [Stu release];
- }
- return 0;
- }
--------------------------------------------------------------------------------------------------------------- ---------with @property and @synthesize are:
Student.h
[CPP]View Plaincopy
- //
- //Student.h
- // Property
- //
- //Created by rio.king on 13-8-25.
- //Copyright (c) 2013 rio.king. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- @interface Student:nsobject
- {
- int Age ;
- int No;
- }
- //When the compiler encounters @property, it automatically expands into getter and setter declarations
- @property int age ;
- @property int No;
- @end
Student.m
[CPP]View Plaincopy
- //
- //STUDENT.M
- // Property
- //
- //Created by rio.king on 13-8-25.
- //Copyright (c) 2013 rio.king. All rights reserved.
- //
- #import "Student.h"
- @implementation Student
- //@synthesize will automatically generate getter and setter implementations
- //@synthesize will access the variable with the same name by default, Age,no,height
- //If a variable with the same name is not found, a private variable age,no,height with the same name is automatically generated internally ,
- //So these variables in the Student.h can also be omitted from writing.
- @synthesize Age,no;
- @end
Main.m
[CPP]View Plaincopy
- //
- //MAIN.M
- // Property
- //
- //Created by rio.king on 13-8-25.
- //Copyright (c) 2013 rio.king. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Student.h"
- int Main (int argc, const char * argv[])
- {
- @autoreleasepool {
- //Insert code here ...
- Student *stu = [[Student alloc] init];
- Stu.age = 100;
- NSLog (@"Age is%i", Stu.age);
- [Stu release];
- }
- return 0;
- }
A few notes:
1. In Xcode4.5 and later versions, you can omit @synthesize, the compiler will automatically help you add getter and setter method implementation, and by default will go to access
_age This member variable, if the _age member variable is not found, a private member variable called _age is automatically generated.
2. Video teaching in the proposed variable name with the "_" prefix as the beginning, but I see big Nerd that book is not used, the individual is more accustomed to the big Nerd that way, so the variable name is not prefixed. Y^o^y
Excerpt from: http://blog.csdn.net/chaoyuan899/article/details/10310719