Skip to content
Search

ObjectiveC:Synthesize

lastmod 2018-10-12created 2018-10-121 edits

Automatically generates the setter and getter.

How to use

Header file:

#import <Cocoa/Cocoa.h>
 @interface Model : NSObject {
    NSString *data;
}
@property(copy, readwrite) NSString *data;
@end

Implementation file:

#import "Model.h"
 @implementation Model
@synthesize data;
@end

Code written as above can now be used from other objects as follows.

#import <Foundation/Foundation.h>
#import "Model.h"

int main(int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Model * model = [[Model alloc] init];

    // synthesize를 통해 생성된 setter, getter사용법
    [model setData:@"Hello Eye!"];
    NSLog([model data]); // Hello Eye!

    // dot 문법 사용하기
    model.data = @"Bye Eye!";
    NSLog(model.data); // Bye Eye!

    [pool drain];
    return 0;
}

See also

author Zer0 sitemap