2012년 7월 26일 목요일

Objective-C 2.0에서 property / synthesize

Objective-C의 OOP에서 특별한 문법이 있다면 바로 property와 synthesize라고 생각한다. 멤버변수(인스턴스)로의 접근 권한을 설정하고 이 멤버에 값을 세팅하는 규칙을 표준적인 방법으로 정의해 둠으로써 클래스의 사용 방법을 인터페이스(interface, 헤더의 역활?)화 해 준다.

보통 property와 synthesize의 사용 방법은 이랬다.
@interface MyClass : NSObject {
    int value;
    NSString *string;
    NSMutableArray *list;
}

@property (assign) int value;
@property (copy) NSString *string;
@property (retain) NSMutableArray *list;

@end


@implementation MyClass

@synthesize value, string, list;

@end
클래스 정의(interface)에서 멤버 인스턴스를 선언하고 property를 이용해 한번 더 인스턴스 접근에 관한 것을 코딩한다. 그리고 클래스 구현부(implementation)에서 synthesize를 이용해 각 property의 getter/setter를 생성 할 수 있다.

이 글은 property와 synthesize에 대해 설명하는 것이 아니니 만큼 상세한 설명은 생략하고 본론으로 들어가겠다.

Objective-C 2.0 부터는 아래와 같은 식으로 코딩 할 수도 있다.
@interface MyClass : NSObject

@property (assign) int value;
@property (copy) NSString *string;
@property (retain) NSMutableArray *list;

@end


@implementation MyClass

@synthesize value, string, list;

@end
뭐가 빠졌는지 알 수 있을 것이다. 바로 멤버 인스턴스 선언이 빠져있고 그냥 property와 synthesize만 이용하고 있다. 전통적인 방법을 이용하고 있었다면 이게 제대로 돌아갈지 의문일 것이다.

하지만 최신의 Xcode에서 빌드하고 있다면 위와 같은 식으로 사용해도 전혀 문제가 없다. Objective-C 2.0 문법을 지원하는 Xcode에서는 자동으로 synthesize에서 인스턴스 멤버를 생성해 주기 때문이다.

The Objective-C Programming Language의 한 부분

이런 문법의 지원이 얼마나 코드를 줄여줄지는 생각해 보지 않아도 될 정도이다. 실제로 하나의 클래스에 수십개의 프로퍼티를 코딩 한 적이 있었는데, 만약 이런 축약형태의 문법이 지원되지 않았다면 헤더(interface 정의부)의 라인수가 거의 두배로 늘어났을 것이다.

참고로 인스턴스와 프로퍼티의 이름을 다르게 해서 연결해 주는 방법도 지원된다. 위의 코드에서 synthesize 부분만 약간 변형해 보자면...
@synthesize value;
@synthesize string = _string;
@synthesize list = myArrayListInstance;
이렇게 하면 자동으로 value, _string, myArrayListInstance 라는 인스턴스 멤버가 클래스에 정의된 것 처럼 동작한다.

Objective-C도 점점 지원되는 새로운 문법이 추가되고 있어서 점점 편해질 것이다. 이 외의 차세대 Objective-C에서 지원되는 문법을 보면 스크립트언어식을 많이 따라하는 것 같은데 굉장히 기대된다. ;-)

댓글 7개 :

익명 :

써먹어야지^^
좋은거 배웠습니다

익명 :

I absolutelу love your ѕite.. Very nice colors & theme.

Diԁ you build thiѕ site yourself? Pleаse reply back as I'm hoping to create my own site and want to know where you got this from or exactly what the theme is called. Kudos!

Review my page - fast payday loans

익명 :

Keep this goіng plеаse, gгeat job!



Tаkе а look at mу wеbраge personal loans

익명 :

Hellо Therе. I found your blog uѕing msn.

This is an extremelу well wгіtten аrticle.
ӏ'll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will definitely return.

Also visit my website :: payday loans

익명 :

I paу a quick visit eаch day a feω blogs anԁ sіtes to read aгticles, eхcept this weblog presentѕ qualіtу
based posts.

Му ωebsite: payday loans uk
My web site > payday loans uk

익명 :

It's going to be finish of mine day, however before ending I am reading this great piece of writing to increase my know-how.

my web blog ... payday loan

익명 :

Ι am extгemelу іmpгеssеd with your writing skills as ωell as with thе laуout on уouг
ωеblog. Is this а ρaid theme or ԁid you customizе it yourself?
Anyway kеep up the nice qualitу wгiting, it's rare to see a nice blog like this one these days.

Feel free to visit my web-site payday loans