------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
Compiler attributes @property and @synthesize
1. @property
@property can automatically generate setter and getter declarations for a member variable.
Create a new project and add the person class.
Person.h
////Person.h//Zijia////Created by Zou on 5/10/15.//Copyright (c) __mycompanyname__. All rights reserved.//#import<Foundation/Foundation.h>@interfaceperson:nsobject{int_age; Double_height; }
@propertyintAge ;- (void) SetHeight: (Double) height;- (Double) height;@end
When the compiler encounters @property will automatically expand the corresponding setter and Getter Declaration, here @property int age will automatically expand into:
-(void) Setage: (int) age; -(int) age;
Here you can check to see if you can call the set and get methods in MAIN.M.
Main.m
#import<Foundation/Foundation.h>#import "Person.h"intMainintargcConst Char*argv[]) {@autoreleasepool { person*p = [PersonNew]; [P Setage:Ten]; intA = [P age];//int a = [P age];NSLog (@"Age is %d", a); } return 0;}
With a statement, there should be an implementation.
Person.m
#import "Person.h"@implementation Person- (void) Setage: (int) age{_age=Age ; }- (int) age{return_age;}- (void) SetHeight: (Double) height{_height=height;}- (Double) height{return_height;}@end
Here the object p calls the set and get methods to see if the compilation can pass.
Printing results:
This proves that @property does generate setter and getter declarations for member variables.
So here the member variable _height setter and getter Declaration can also be written as @property double height;
Double height; // -(void) SetHeight: (double) height; // -(double) height; @end
Second, @synthesize
@synthesize can automatically generate a member variable setter and Getter implementation section, and access the member variable "_ member variable".
Person.m
#import " Person.h " @implementation Person @synthesize age=_age; @synthesize height=_height;
-(void) test
{
NSLog (@ "age=%d,_age=%d", age,_age);
}@end
@synthesize age = _age; The _age to the right of the equals sign indicates that the values set by the future set method will be assigned to the member variable _age, where you can add a member variable int age;
Person.h
#import <Foundation/Foundation.h>@interface person:nsobject{ int _age; int Age ; Double _height; int Double height;-(void) test; @end
If the PERSON.M is written in @synthesize age = age, then the set method assigns the value to the member variable age in the future, and can be verified by writing a test method.
Main.m
#import <Foundation/Foundation.h>#import"Person.h"int Main (intconstChar * argv[]) { @autoreleasepool { new ]; Ten ]; [P test]; } return 0 ;}
Compile and run it here:
By printing the results we found that the value of age has not changed, or the default value of 0, and the value of _age into 10, here we put person.m inside the @synthesize age=_age, written @synthesize age=age, re-print the results found:
The age value becomes 10, and the _age value is 0, which shows how the setter method changes that member variable, depending on what the variable behind the @synthesize equals sign is.
It is found here that using compiler features @property and @synthesize can avoid writing a lot of repetitive code, and here it can be more streamlined.
Add a new dog class.
Dog.h
#import <Foundation/Foundation.h>@interfaceint age ; @end
There is no need to write the member variables here, dog.m do not write the implementation code, @property int age This line of code did three things:
First, generate a declaration of getter and setter;
Second, a _age member variable is generated;
Thirdly, the realization of getter and setter is generated;
Here you can verify:
Main.m
#import<Foundation/Foundation.h>#import "Person.h"#import "Dog.h"intMainintargcConst Char*argv[]) {@autoreleasepool { person*p = [PersonNew]; P.age=5; NSLog (@"age=%d", P.age); } return 0;}
Print the results:
This is the proof of the previous statement. It is important to note that age by default is a private member variable, the subclass is inaccessible, and if you want the subclass to be accessible, you need to specify the member variable.
#import <Foundation/Foundation.h>@interface dog:nsobject{ int int age ; @end
@property age; This member variable is accessed by default and is automatically generated when the @private type is not available.
@synthesize age; By default, this member variable is accessed, and if not, an age of type @private is automatically generated.
Since Xcode 4.x, @property features have covered the functionality of the @synthesize, @synthesize can be omitted to write.
If the setter method is implemented manually, the compiler automatically generates getter methods and underlined member variables.
If you implement the Getter method manually, the compiler automatically generates a setter method and an underlined member variable.
If the setter and getter methods are implemented manually, the compiler does not generate getter and setter methods, nor does it produce an underlined member variable.
Dark Horse Programmer---objective-c basic learning---compiler features @property and @synthesize