Altruistic Programmer's Blog (KR)

이타주의 프로그래머의 블로그

Objective-C 2.0에서 프라이빗 프로퍼티 정의하기

without comments

클래스의 헤더에 프로퍼티를 숨기고 싶은 경우에는 클래스 익스텐션을 사용할 수 있다.

// A.h
@interface A : NSObject {

}
-(void)setXXX:(int)i;
-(int)xxx;
@end

// A.m
#import "A.h"
@interface A()
@property int m; // HERE
@end

@implementation A
@synthesize m; // AND HERE
-(void)setXXX:(int)i {
self.m = i;
}
-(int)xxx {
return self.m;
}
@end

카테고리보다 클래스 익스텐션이 좋은 이유는

  1. 프로퍼티를 정의할 수 있다
  2. 메소드를 선언만하고 구현하지 않으면 에러가 난다.

Written by muscly

April 29th, 2011 at 8:17 pm

Posted in 프로그래밍

Leave a Reply