Monday, June 11, 2012

Synthesized accessor methods



automatic way to generate setter/getter methods.


- - - In ExampleClass.h - - -
#import <Foundation/Foundation.h>
@interface ExampleClass : NSObject { int age, weight; }
@property int age, weight; // declared to be modifiable
-(void) print;
@end

- - - in ExampleClass.m - - -
#import "ExampleClass.h"
@implementation ExampleClass
@synthesize age, weight;
-(void) print{ NSLog(@"I am @i years old and weigh %i pounds", age, weight); }
@end

- - - in MainProgram.m - - -
#import <Foundation/Foundation.h>
#import "ExampleClass.h"
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
ExampleClass *ClassInstanceVariable = [[ExampleClass alloc] init];
ClassInstanceVariable.age = 12;
ClassInstanceVariable.weight = 140;

[ ClassInstanceVariable print ];
[ ClassInstanceVariable release]; // related to memory management.
[pool drain];
return 0;
}

- BE


No comments:

Post a Comment