In Objective-c, you use @property to identify the attribute (typically an instance variable). Use the @synthesize to identify the variables declared in the implementation file, allowing the system to automatically generate the Set method and get method.
In other words, @property and @synthesize are paired, allowing the system to automatically generate the Setup method and get methods.
Cases:
Test.h
[CPP]View Plaincopy
- #import <Foundation/Foundation.h>
- @interface Test:nsobject {
- int IntX;
- int inty;
- }
- @property int intx,inty;
- -(void) print;
- @end
Test.m
[CPP]View Plaincopy
- #import "Test.h"
- @implementation Test
- @synthesize Intx,inty;
- -(void) Print {
- NSLog (@"intx+inty=%d", Intx+inty);
- }
- @end
Classtest.m
[CPP]View Plaincopy
- #import <Foundation/Foundation.h>
- #import "Test.h"
- int main ( int argc, const char* argv[]) {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
- Test *test = [[Test alloc]init];
- [Test setintx:1];
- [Test setinty:1];
- [Test print];
- [Test release];
- [Pool drain];
- return 0;
- }
Program Run Result:
- Test.inty = 1;
It is equivalent to:
[CPP] view plaincopy
- [Test setintx:1];
- [Test setinty:1];
At the same time, need to pay special attention: Copyright Notice: This article for Bo Master original article, without Bo Master permission not reproduced.
Objective-c @property and @synthesize