반응형
Tristan O’Tierney이 운영하는 Objective-C Tutorial의 한국어 버전을 번역했다. Tristan에게 허가를 구했고, 내가 번역한 파일은 그가 사용할 수 있도록 했다. Objective-C의 문법을 빠르게 그리고 간략하게 잘 설명해서 여러사람에게 도움이 될 것 같아 번역했다.

HTML버전은 http://kjune.com/objc/objective-c-kor.html
원본 사이트 : http://www.otierney.net/objective-c.html.ko


개요


    시작하기

    이 튜토리얼 다운로드

    • 이 가이드에 사용된 소스 코드와 Makefile은 objc.tar.gz에서 다운 받을 수 있다. 이 튜토리얼의 예제들은 Steve Kochan씨가 쓴 Programming in Objective-C에서 인용한 것이다. 더 자세한 정보와 예제가 필요하다면, 이 책을 참조하면 도움이 될 것이다. 이 사이트에서 사용된 예제는 그의 허가를 받고 사용한 것이므로, 다른 곳에 무단 복제를 금지한다.

    환경설정

    • Linux/FreeBSD: GNUStep를 설치
      • GNUStep 어플리케이션을 빌드하기 위해서는 먼저 /usr/GNUstep/System/Makefiles/GNUstep.sh에 있는 GNUstep.sh 스크립트를 실행한다. 경로는 시스템에 따라 다를 수 있다. /usr, 혹은 /usr/lib, 혹은 /usr/local를 찾아볼 것. 만약 사용하는 셀이 csh/tcsh 기반의 쉘이라면, GNUStep.csh를 대신 실행시키면 된다. 이 스크립트를 쉘 설정파일인 .bashrc 이나 .cshrc에 추가하는 것을 추천한다.
    • Mac OS X: XCode설치
    • Windows NT 5.X: 먼저 cygwin 이나 mingw를 설치한 후에 GNUStep을 설치

    머리말

    • 이 튜토리얼은 사용자가 기본적인 C 지식이 있다고 전제하고 설명을 하고 있다. C의 데이터형, 함수, 반환값, 포인터와 기본적인 메모리 관리등이 이에 해당된다. 아직 여기까지 내용을 모른다면, 다음의 책을 참조하길 바란다. The C Programming Language. 이 책은 C언어를 만든 사람들이 만든 바로 그 책이다.
    • 오브젝티브-C는 C의 파생언어여서 C의 많은 기능을 차용받았다. 하지만 C로 부터 오지 않은 다른 부분들도 있다.
    • nil: C/C++ 에서는 NULL이라고 사용하는 데, 오브젝티브-C에서는 nil이라고 한다. 차이점은 nil에게는 메시지를 전달할 수 있다는 점이다. (예를 들면 이런 식으로 [nil message];) 이렇게 해도 괜찮다. 반면 C에서 NULL에게는 이렇게 할 수 없다.
    • BOOL: C에는 공식적인 불린 형이 없다. 사실 오브젝티브-C에서도 공식적인 불린형은 없다. 하지만 불린형은 Foundation classes에 포함이 되어서 (NSObject.h를 임포트 함으로써) 그리고 nil도 역시 이 헤더파일로부터 추가된다. 오브젝티브-C에서의 BOOL은 YES나 NO라는 2개의 모드를 갖는다. (TRUE나 FALSE가 아니다.)
    • #import 대 #include: 위의 hello world 예제에서 눈치챘겠지만, 파일을 포함할 때 #import를 사용한다. #import는 gcc 컴파일러가 지원한다. 다만 #include를 더 선호하기 때문에 덜 사용되게 될 뿐이다. #import는 사용자가 만든 .h파일의 위와 아래에 #ifndef #define #endif 블럭을 붙이는 것과 같은 일을 한다. 이 블럭을 붙이는 건 정말 멍청한 짓이라고 생각하고, 아마 다른 모든 개발자들도 동의할 것이다. 따라서 그냥 #import를 사용하면 된다. 걱정할 필요도 없고, gcc에서 #import를 제거한다고 해도, 다른 오브젝티브-C 개발자들이 그런 일이 생기지 못하게 할 것이다. 공식적으로 애플에서 #import를 자신들의 코드에 사용하니, 만약 #import가 gcc에서 사라진다고 해도, 애플에서는 gcc를 변형한 버전이라도 출시할 것이다.
    • 비록 메시지가 조금 더 특별한 특성을 가지긴 해도, 메소드와 메시지란 단어는 오브젝티브-C에서는 서로 교환 가능한 말이다. 메시지는 동적으로 다른 객체에게 포워딩 될 수 있다. 오브젝티브-C에서 객체의 메시지를 호출한다는 것은 객체가 그 메시지를 구현한 다는 것을 의미하는 것이 아니다. 단지 객체가 메시지가 전달되었을 때 어떻게 반응해야 하는 지를 안다는 뜻이다. 그 방식은 직접 구현을 하거나 혹은 처리방법을 아는 다른 객체에 메시지를 포워딩하는 방식을 사용할 수 있다.

    hello world 만들기

    • hello.m
      1. #import <stdio.h>  
      2.   
      3. int main( int argc, const char *argv[] ) {  
      4. printf( "hello world\n" );  
      5. return 0;  
      6. }  
    • 결과
      hello world
    • 오브젝티브-C에서는 #include 대신에 #import를 사용한다.
    • 오브젝티브-C의 기본 확장자는 .m이다.

클래스 만들기

    인터페이스 @interface

    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Fraction.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. @interface Fraction: NSObject {  
      4. int numerator;  
      5. int denominator;  
      6. }  
      7.   
      8. -(void) print;  
      9. -(void) setNumerator: (int) n;  
      10. -(void) setDenominator: (int) d;  
      11. -(int) numerator;  
      12. -(int) denominator;  
      13. @end  
    • NSObject: NeXTStep Object의 줄임말. 이제는 OpenStep으로 이름이 바꿔서 의미가 맞지 않긴 하다.
    • 상속, 다형성, 그리고 다른 객체 지향 프로그래밍의 기능들은 Class: Parent와 같은 형태로 표현하면 된다. 위의 예제에서는 Fraction: NSObject 부분이다.
    • 인스턴스 변수들은 @interface Class: Parent { …. }의 안쪽으로 들어간다.
    • 아직 접근 레벨이 정해져 있지 않다. (protected, public, private). 기본은 protected이다. 접근 셋팅하는 법은 뒤에 더 다루겠다.
    • 멤버 함수 뒤에는 인스턴스 메소드가 나온다. 포멧은 scope (returnType) methodName: (parameter1Type) parameter1Name;과 같다.
      • 스코프(scope)는 매스드가 클래스에서 사용하는지 인스턴스에서 사용하는 지를 판별한다. 인스턴스에서 사용하는 메소드는 -를, 클래스에서 사용하는 메소드는 +로 시작한다.
    • 인터페이스는 @end로 끝난다.

    @implementation

    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Fraction.m
      1. #import "Fraction.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation Fraction  
      5. -(void) print {  
      6. printf( "%i/%i", numerator, denominator );  
      7. }  
      8.   
      9. -(void) setNumerator: (int) n {  
      10. numerator = n;  
      11. }  
      12.   
      13. -(void) setDenominator: (int) d {  
      14. denominator = d;  
      15. }  
      16.   
      17. -(int) denominator {  
      18. return denominator;  
      19. }  
      20.   
      21. -(int) numerator {  
      22. return numerator;  
      23. }  
      24. @end  
    • 클래스의 구현부분은 @implementation ClassName 으로 시작하고, @end으로 끝난다.
    • 모든 정의된 메소드들은 인터페이스에서 정의된 것과 유사한 형태로 구현된다.

    모두 합하기

    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • main.m
      1. #import <stdio.h>  
      2. #import "Fraction.h"  
      3.   
      4. int main( int argc, const char *argv[] ) {  
      5. // create a new instance  
      6. Fraction *frac = [[Fraction alloc] init];  
      7.   
      8. // set the values  
      9. [frac setNumerator: 1];  
      10. [frac setDenominator: 3];  
      11.   
      12. // print it  
      13. printf( "The fraction is: " );  
      14. [frac print];  
      15. printf( "\n" );  
      16.   
      17. // free memory  
      18. [frac release];  
      19.   
      20. return 0;  
      21. }  
    • 결과
      The fraction is: 1/3
    • Fraction *frac = [[Fraction alloc] init];
      • 이 한 줄에는 몇 가지 중요한 것들이 있다.
      • 오브젝티브-C에서 메소드는 [object method]와 같은 형태로 호출된다. C++에서는 object->method() 와 같은 형태로 호출되는 것과 유사하다.
      • 오브젝티브-C에는 값 타입이 없다. 다시 말해 C++처럼 Fraction frac; frac.print(); 이런 식으로 코딩하지 않는 다는 뜻이다. 오브젝티브-C에서는 반드시 객체를 포인터를 통해서 다뤄야 한다.
      • 이 한 줄에서 실제로는 두 가지 일이 일어난다: [Fraction alloc]은 Fracion 클래스의 alloc 메소드를 호출한다. 이 것은 메모리를 할당하는 것과 유사하다. 이 단계에서 메모리 할당이 일어난다.
      • [object init]은 객체 안에 있는 여러 변수들의 값을 초기화하는 생성자 호출이다. 이 메소드는 [Fraction alloc]에 의해서 반환된 인스턴스가 호출한다. 보통 이 과정은 다음과 같은 형태로 한 줄로 이뤄진다. Object* var = [[Object alloc]init];
    • [frac setNumerator: 1]은 간단하다. frac객체의 setNumerator 함수를 호출하면서 인자 1을 전달한다.
    • 다른 C의 변종들과 마찬가지로, 메모리를 해제하는 구조가 있다. 여기에서는 release를 통해서 처리되었는데, 이는 NSObject에서 상속 받은 것이다. 이에 대해서는 뒤에서 더 자세히 다루겠다.

세부적인 내용들

    매개변수를 여러 개 갖는 경우

    • 지금까지는 매개변수가 여러 개인 함수는 나오지 않았다. 처음에 보면 방식이 직관적이지 않다는 생각이 드는데, 이 것은 스몰토크(Smalltalk)로 부터 차용해온 것이다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Fraction.h
      1. ...  
      2. -(void) setNumerator: (int) n andDenominator: (int) d;  
      3. ...  
    • Fraction.m
      1. ...  
      2. -(void) setNumerator: (int) n andDenominator: (int) d {  
      3. numerator = n;  
      4. denominator = d;  
      5. }  
      6. ...  
    • main.m
      1. #import <stdio.h>  
      2. #import "Fraction.h"  
      3.   
      4. int main( int argc, const char *argv[] ) {  
      5. // create a new instance  
      6. Fraction *frac = [[Fraction alloc] init];  
      7. Fraction *frac2 = [[Fraction alloc] init];  
      8.   
      9. // set the values  
      10. [frac setNumerator: 1];  
      11. [frac setDenominator: 3];  
      12.   
      13. // combined set  
      14. [frac2 setNumerator: 1 andDenominator: 5];  
      15.   
      16. // print it  
      17. printf( "The fraction is: " );  
      18. [frac print];  
      19. printf( "\n" );  
      20.   
      21. // print it  
      22. printf( "Fraction 2 is: " );  
      23. [frac2 print];  
      24. printf( "\n" );  
      25.   
      26. // free memory  
      27. [frac release];  
      28. [frac2 release];  
      29.   
      30. return 0;  
      31. }  
    • 결과
      The fraction is: 1/3
      Fraction 2 is: 1/5
    • 메소드는 실제로 setNumerator:andDenominator:를 호출했다.
    • 더 많은 인자를 전달하고자 할 때는 두번째 인자를 전달하는 것과 같은 방식으로 하면 된다. method:label1:label2:label3: 그런 다음 호출할 때에는 [obj method: param1 label1: param2 label2: param3 label3: param4]식으로 하면 된다.
    • 레이블은 써도 되고 안써도 된다. 메소드 이름을 method:::라고 해도 된다. 레이블 이름을 쓰지 않으면 이렇게 된다. 그리고 a: 를 인자를 구분하기 위해서 사용한다. 하지만, 이 방식을 추천하진 않는다.

    생성자

    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Fraction.h
      1. ...  
      2. -(Fraction*) initWithNumerator: (int) n denominator: (int) d;  
      3. ...  
    • Fraction.m
      1. ...  
      2. -(Fraction*) initWithNumerator: (int) n denominator: (int) d {  
      3. self = [super init];  
      4.   
      5. if ( self ) {  
      6. [self setNumerator: n andDenominator: d];  
      7. }  
      8.   
      9. return self;  
      10. }  
      11. ...  
    • main.m
      1. #import <stdio.h>  
      2. #import "Fraction.h"  
      3.   
      4. int main( int argc, const char *argv[] ) {  
      5. // create a new instance  
      6. Fraction *frac = [[Fraction alloc] init];  
      7. Fraction *frac2 = [[Fraction alloc] init];  
      8. Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];  
      9.   
      10. // set the values  
      11. [frac setNumerator: 1];  
      12. [frac setDenominator: 3];  
      13.   
      14. // combined set  
      15. [frac2 setNumerator: 1 andDenominator: 5];  
      16.   
      17. // print it  
      18. printf( "The fraction is: " );  
      19. [frac print];  
      20. printf( "\n" );  
      21.   
      22. printf( "Fraction 2 is: " );  
      23. [frac2 print];  
      24. printf( "\n" );  
      25.   
      26. printf( "Fraction 3 is: " );  
      27. [frac3 print];  
      28. printf( "\n" );  
      29.   
      30. // free memory  
      31. [frac release];  
      32. [frac2 release];  
      33. [frac3 release];  
      34.   
      35. return 0;  
      36. }  
    • 결과
      The fraction is: 1/3
      Fraction 2 is: 1/5
      Fraction 3 is: 3/10
    • @interface선언은 다른 함수들과 동일하다.
    • @implementation에 새로운 키워드 super가 등장했다.
      • 자바와 마찬가지로 오브젝티브-C에는 오직 한 개의 부모 클래스만을 가진다.
      • 부모의 생성자를 호출하기 위해서는 [super init]을 호출한다. 적절하게 상속을 하기 위해서 반드시 해야 한다.
      • 여기에 self라는 또 다른 키워드가 등장한다. self는 자기 자신에 대한 인스턴스를 가리킨다. self는 자바나 C++에서의 this와 유사하다.
    • if ( self ) 라고 쓰는 것은 if ( self != nil ) 와 동일하다. 부모 클래스의 생성자로부터 성공적으로 새 객체를 생성해서 받아왔다는 것을 보장하기 위해서 사용한 것이다. 오브젝티브-C에서의 nil은 C/C++의 NULL과 같다. NSObject를 포함함으로써 추가되었다.
    • 값들을 초기화 한 후에는 return self;를 통해서 자기 자신의 객체를 반환해야 한다.
    • 초기 생성자는 -(id) init; 이다.
    • 오브젝티브-C에서의 생성자는 단순한 “init” 메소드이다. C++이나 자바에서처럼 특별한 생성자가 있는 것은 아니다.

    접근 권한

    • 기본 권한은 @protected이다.
    • 자바에서는 개별 메소드와 변수 앞에 public/private/protect라고 표시를 한다. 오브젝티브-C는 C++과 유사한 방식이다.
    • Access.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. @interface Access: NSObject {  
      4. @public  
      5. int publicVar;  
      6. @private  
      7. int privateVar;  
      8. int privateVar2;  
      9. @protected  
      10. int protectedVar;  
      11. }  
      12. @end  
    • Access.m
      1. #import "Access.h"  
      2.   
      3. @implementation Access  
      4. @end  
    • main.m
      1. #import "Access.h"  
      2. #import <stdio.h>  
      3.   
      4. int main( int argc, const char *argv[] ) {  
      5. Access *a = [[Access alloc] init];  
      6.   
      7. // works  
      8. a->publicVar = 5;  
      9. printf( "public var: %i\n", a->publicVar );  
      10.   
      11. // doesn't compile  
      12. //a->privateVar = 10;  
      13. //printf( "private var: %i\n", a->privateVar );  
      14.   
      15. [a release];  
      16. return 0;  
      17. }  
    • 결과
      public var: 5
    • 위에서 본 바와 같이 C++에서는 private: [list of vars] public: [list of vars]와 같이 표시하는 것을 여기에서는 @private, @protected, 등등으로 표시한다.

    클래스 수준 접근성

    • 종종 클래스 수준에서 변수와 함수를 조절할 수 있는 것이 필요할 때가 있다. 예를 들어, 객체가 인스턴스화 된 횟수를 추적하고자 할 때와 같은 경우이다.
    • ClassA.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. static int count;  
      4.   
      5. @interface ClassA: NSObject  
      6. +(int) initCount;  
      7. +(void) initialize;  
      8. @end  
    • ClassA.m
      1. #import "ClassA.h"  
      2.   
      3. @implementation ClassA  
      4. -(id) init {  
      5. self = [super init];  
      6. count++;  
      7. return self;  
      8. }  
      9.   
      10. +(int) initCount {  
      11. return count;  
      12. }  
      13.   
      14. +(void) initialize {  
      15. count = 0;  
      16. }  
      17. @end  
    • main.m
      1. #import "ClassA.h"  
      2. #import <stdio.h>  
      3.   
      4. int main( int argc, const char *argv[] ) {  
      5. ClassA *c1 = [[ClassA alloc] init];  
      6. ClassA *c2 = [[ClassA alloc] init];  
      7.   
      8. // print count  
      9. printf( "ClassA count: %i\n", [ClassA initCount] );  
      10.   
      11. ClassA *c3 = [[ClassA alloc] init];  
      12.   
      13. // print count again  
      14. printf( "ClassA count: %i\n", [ClassA initCount] );  
      15.   
      16. [c1 release];  
      17. [c2 release];  
      18. [c3 release];  
      19.   
      20. return 0;  
      21. }  
    • 결과
      ClassA count: 2
      ClassA count: 3
    • static int count = 0; 이렇게 클래스 변수를 선언한다. 이 방법이 변수를 보관하는 가장 좋은 방법은 아니다. 자바에서 구현하는 것이 조금 더 좋은 해결책으로 생각되지만, 이 방법도 된다.
    • +(int) initCount; 이 함수가 실제로 객수를 반환한다. 눈여겨볼 차이점이 하나가 있는데 형 앞에 -를 쓴 대신 +를 사용했다. +는 클래스 레벨 함수라는 뜻이다.
    • 클래스 레벨의 변수를 접근 하는 방식은 일반 멤버 변수를 접근 하는 방식과 동일하다. ClassA의 생성자에 있는 count++를 보면 된다.
    • +(void) initialize 메소드는 오브젝티브-C가 프로그램을 시작할 때 호출된다. 그리고 이 모든 클래스에서 호출된다. 따라서 이 예제의 count와 같은 클래스 레벨의 변수를 초기화 하기에 좋은 위치이다.

    예외처리

    • 참고: 예외 처리는 오직 맥 OS X 10.3에서 지원됩니다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • CupWarningException.h
      1. #import <Foundation/NSException.h>  
      2.   
      3. @interface CupWarningException: NSException  
      4. @end  
    • CupWarningException.m
      1. #import "CupWarningException.h"  
      2.   
      3. @implementation CupWarningException  
      4. @end  
    • CupOverflowException.h
      1. #import <Foundation/NSException.h>  
      2.   
      3. @interface CupOverflowException: NSException  
      4. @end  
    • CupOverflowException.m
      1. #import "CupOverflowException.h"  
      2.   
      3. @implementation CupOverflowException  
      4. @end  
    • Cup.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. @interface Cup: NSObject {  
      4. int level;  
      5. }  
      6.   
      7. -(int) level;  
      8. -(void) setLevel: (int) l;  
      9. -(void) fill;  
      10. -(void) empty;  
      11. -(void) print;  
      12. @end  
    • Cup.m
      1. #import "Cup.h"  
      2. #import "CupOverflowException.h"  
      3. #import "CupWarningException.h"  
      4. #import <Foundation/NSException.h>  
      5. #import <Foundation/NSString.h>  
      6.   
      7. @implementation Cup  
      8. -(id) init {  
      9. self = [super init];  
      10.   
      11. if ( self ) {  
      12. [self setLevel: 0];  
      13. }  
      14.   
      15. return self;  
      16. }  
      17.   
      18. -(int) level {  
      19. return level;  
      20. }  
      21.   
      22. -(void) setLevel: (int) l {  
      23. level = l;  
      24.   
      25. if ( level > 100 ) {  
      26. // throw overflow  
      27. NSException *e = [CupOverflowException  
      28. exceptionWithName: @"CupOverflowException"  
      29. reason: @"The level is above 100"  
      30. userInfo: nil];  
      31. @throw e;  
      32. else if ( level >= 50 ) {  
      33. // throw warning  
      34. NSException *e = [CupWarningException  
      35. exceptionWithName: @"CupWarningException"  
      36. reason: @"The level is above or at 50"  
      37. userInfo: nil];  
      38. @throw e;  
      39. else if ( level < 0 ) {  
      40. // throw exception  
      41. NSException *e = [NSException  
      42. exceptionWithName: @"CupUnderflowException"  
      43. reason: @"The level is below 0"  
      44. userInfo: nil];  
      45. @throw e;  
      46. }  
      47. }  
      48.   
      49. -(void) fill {  
      50. [self setLevel: level + 10];  
      51. }  
      52.   
      53. -(void) empty {  
      54. [self setLevel: level - 10];  
      55. }  
      56.   
      57. -(void) print {  
      58. printf( "Cup level is: %i\n", level );  
      59. }  
      60. @end  
    • main.m
      1. #import "Cup.h"  
      2. #import "CupOverflowException.h"  
      3. #import "CupWarningException.h"  
      4. #import <Foundation/NSString.h>  
      5. #import <Foundation/NSException.h>  
      6. #import <Foundation/NSAutoreleasePool.h>  
      7. #import <stdio.h>  
      8.   
      9. int main( int argc, const char *argv[] ) {  
      10. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
      11. Cup *cup = [[Cup alloc] init];  
      12. int i;  
      13.   
      14. // this will work  
      15. for ( i = 0; i < 4; i++ ) {  
      16. [cup fill];  
      17. [cup print];  
      18. }  
      19.   
      20. // this will throw 예외처리  
      21. for ( i = 0; i < 7; i++ ) {  
      22. @try {  
      23. [cup fill];  
      24. } @catch ( CupWarningException *e ) {  
      25. printf( "%s: ", [[e name] cString] );  
      26. } @catch ( CupOverflowException *e ) {  
      27. printf( "%s: ", [[e name] cString] );  
      28. } @finally {  
      29. [cup print];  
      30. }  
      31. }  
      32.   
      33. // throw a generic exception  
      34. @try {  
      35. [cup setLevel: -1];  
      36. } @catch ( NSException *e ) {  
      37. printf( "%s: %s\n", [[e name] cString], [[e reason] cString] );  
      38. }  
      39.   
      40. // free memory  
      41. [cup release];  
      42. [pool release];  
      43. }  
    • 결과
      Cup level is: 10
      Cup level is: 20
      Cup level is: 30
      Cup level is: 40
      CupWarningException: Cup level is: 50
      CupWarningException: Cup level is: 60
      CupWarningException: Cup level is: 70
      CupWarningException: Cup level is: 80
      CupWarningException: Cup level is: 90
      CupWarningException: Cup level is: 100
      CupOverflowException: Cup level is: 110
      CupUnderflowException: The level is below 0
    • NSAutoreleasePool은 메모리 관리 클래스이다. 지금은 일단 넘어가자.
    • 전달 받은 예외를 처리하기 위해서 NSException을 확장할 필요는 없다. 간단히 id를 사용하면 된다. @catch ( id e ) { … }
    • 마지막 부분에 finally 블럭이 있다. 자바와 유사하게 작동하는데, finally 블럭에 있는 구문은 반드시 호출되게 되어있다.
    • Cub.m에 있는 문자열 @”CupOverflowException”은 NSString 상수 객체이다. 오브젝티브-C에서의 @표시는 언어의 확장을 의미한다. C스트링은 C나 C++과 유사하다. 문자열 상수이고 모두 char*로 만들어져 있다.

상속, 다형성, 그리고 다른 객체 지향 프로그래밍의 기능들

    id 형

    • 오브젝티브-C에는 id라는 특별한 형이 있다. 이것은 여러가지 면에서 void*와 유사하게 사용된다. 하지만 반드시 객체에 대해서만 사용된다. 오브젝티브-C는 객체의 메소드를 호출 하는 방식에 있어서 자바나 C++과 다른데, 이 때에 객체의 타입을 알 필요가 없다. 단지 메소드만 있으면 된다. 이것이 오브젝티브-C에서 메시지를 전달하는 방식으로 간주된다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Fraction.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. @interface Fraction: NSObject {  
      4. int numerator;  
      5. int denominator;  
      6. }  
      7.   
      8. -(Fraction*) initWithNumerator: (int) n denominator: (int) d;  
      9. -(void) print;  
      10. -(void) setNumerator: (int) d;  
      11. -(void) setDenominator: (int) d;  
      12. -(void) setNumerator: (int) n andDenominator: (int) d;  
      13. -(int) numerator;  
      14. -(int) denominator;  
      15. @end  
    • Fraction.m
      1. #import "Fraction.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation Fraction  
      5. -(Fraction*) initWithNumerator: (int) n denominator: (int) d {  
      6. self = [super init];  
      7.   
      8. if ( self ) {  
      9. [self setNumerator: n andDenominator: d];  
      10. }  
      11.   
      12. return self;  
      13. }  
      14.   
      15. -(void) print {  
      16. printf( "%i / %i", numerator, denominator );  
      17. }  
      18.   
      19. -(void) setNumerator: (int) n {  
      20. numerator = n;  
      21. }  
      22.   
      23. -(void) setDenominator: (int) d {  
      24. denominator = d;  
      25. }  
      26.   
      27. -(void) setNumerator: (int) n andDenominator: (int) d {  
      28. numerator = n;  
      29. denominator = d;  
      30. }  
      31.   
      32. -(int) denominator {  
      33. return denominator;  
      34. }  
      35.   
      36. -(int) numerator {  
      37. return numerator;  
      38. }  
      39. @end  
    • Complex.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. @interface Complex: NSObject {  
      4. double real;  
      5. double imaginary;  
      6. }  
      7.   
      8. -(Complex*) initWithReal: (double) r andImaginary: (double) i;  
      9. -(void) setReal: (double) r;  
      10. -(void) setImaginary: (double) i;  
      11. -(void) setReal: (double) r andImaginary: (double) i;  
      12. -(double) real;  
      13. -(double) imaginary;  
      14. -(void) print;  
      15.   
      16. @end  
    • Complex.m
      1. #import "Complex.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation Complex  
      5. -(Complex*) initWithReal: (double) r andImaginary: (double) i {  
      6. self = [super init];  
      7.   
      8. if ( self ) {  
      9. [self setReal: r andImaginary: i];  
      10. }  
      11.   
      12. return self;  
      13. }  
      14.   
      15. -(void) setReal: (double) r {  
      16. real = r;  
      17. }  
      18.   
      19. -(void) setImaginary: (double) i {  
      20. imaginary = i;  
      21. }  
      22.   
      23. -(void) setReal: (double) r andImaginary: (double) i {  
      24. real = r;  
      25. imaginary = i;  
      26. }  
      27.   
      28. -(double) real {  
      29. return real;  
      30. }  
      31.   
      32. -(double) imaginary {  
      33. return imaginary;  
      34. }  
      35.   
      36. -(void) print {  
      37. printf( "%_f + %_fi", real, imaginary );  
      38. }  
      39.   
      40. @end  
    • main.m
      1. #import <stdio.h>  
      2. #import "Fraction.h"  
      3. #import "Complex.h"  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. // create a new instance  
      7. Fraction *frac = [[Fraction alloc] initWithNumerator: 1 denominator: 10];  
      8. Complex *comp = [[Complex alloc] initWithReal: 10 andImaginary: 15];  
      9. id number;  
      10.   
      11. // print fraction  
      12. number = frac;  
      13. printf( "The fraction is: " );  
      14. [number print];  
      15. printf( "\n" );  
      16.   
      17. // print complex  
      18. number = comp;  
      19. printf( "The complex number is: " );  
      20. [number print];  
      21. printf( "\n" );  
      22.   
      23. // free memory  
      24. [frac release];  
      25. [comp release];  
      26.   
      27. return 0;  
      28. }  
    • 결과
      The fraction is: 1 / 10
      The complex number is: 10.000000 + 15.000000i
    • 이런 식의 동적인 형 연결을 통해서 여러가지 이득이 있다. 어떤 함수를 호출하기 위해서 그 객체의 형을 반드시 알 필요는 없다. 만약 객체가 메시지에 반응한다면, 그 메소드를 호출할 것이다. 그렇기 때문에 지저분한 캐스팅 문제가 오브젝티브-C에서는 발생하지 않는 것이다. 자바에서는 .intValue() 라는 함수를 호출하려면 먼저 캐스팅을 해야하고, 그 뒤에 메소드를 호출해야 하는 것과는 대조적이다.

    상속, 다형성, 그리고 다른 객체 지향 프로그래밍의 기능들

    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Rectangle.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. @interface Rectangle: NSObject {  
      4. int width;  
      5. int height;  
      6. }  
      7.   
      8. -(Rectangle*) initWithWidth: (int) w height: (int) h;  
      9. -(void) setWidth: (int) w;  
      10. -(void) setHeight: (int) h;  
      11. -(void) setWidth: (int) w height: (int) h;  
      12. -(int) width;  
      13. -(int) height;  
      14. -(void) print;  
      15. @end  
    • Rectangle.m
      1. #import "Rectangle.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation Rectangle  
      5. -(Rectangle*) initWithWidth: (int) w height: (int) h {  
      6. self = [super init];  
      7.   
      8. if ( self ) {  
      9. [self setWidth: w height: h];  
      10. }  
      11.   
      12. return self;  
      13. }  
      14.   
      15. -(void) setWidth: (int) w {  
      16. width = w;  
      17. }  
      18.   
      19. -(void) setHeight: (int) h {  
      20. height = h;  
      21. }  
      22.   
      23. -(void) setWidth: (int) w height: (int) h {  
      24. width = w;  
      25. height = h;  
      26. }  
      27.   
      28. -(int) width {  
      29. return width;  
      30. }  
      31.   
      32. -(int) height {  
      33. return  height;  
      34. }  
      35.   
      36. -(void) print {  
      37. printf( "width = %i, height = %i", width, height );  
      38. }  
      39. @end  
    • Square.h
      1. #import "Rectangle.h"  
      2.   
      3. @interface Square: Rectangle  
      4. -(Square*) initWithSize: (int) s;  
      5. -(void) setSize: (int) s;  
      6. -(int) size;  
      7. @end  
    • Square.m
      1. #import "Square.h"  
      2.   
      3. @implementation Square  
      4. -(Square*) initWithSize: (int) s {  
      5. self = [super init];  
      6.   
      7. if ( self ) {  
      8. [self setSize: s];  
      9. }  
      10.   
      11. return self;  
      12. }  
      13.   
      14. -(void) setSize: (int) s {  
      15. width = s;  
      16. height = s;  
      17. }  
      18.   
      19. -(int) size {  
      20. return width;  
      21. }  
      22.   
      23. -(void) setWidth: (int) w {  
      24. [self setSize: w];  
      25. }  
      26.   
      27. -(void) setHeight: (int) h {  
      28. [self setSize: h];  
      29. }  
      30. @end  
    • main.m
      1. #import "Square.h"  
      2. #import "Rectangle.h"  
      3. #import <stdio.h>  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];  
      7. Square *sq = [[Square alloc] initWithSize: 15];  
      8.   
      9. // print em  
      10. printf( "Rectangle: " );  
      11. [rec print];  
      12. printf( "\n" );  
      13.   
      14. printf( "Square: " );  
      15. [sq print];  
      16. printf( "\n" );  
      17.   
      18. // update square  
      19. [sq setWidth: 20];  
      20. printf( "Square after change: " );  
      21. [sq print];  
      22. printf( "\n" );  
      23.   
      24. // free memory  
      25. [rec release];  
      26. [sq release];  
      27.   
      28. return 0;  
      29. }  
    • 결과
      Rectangle: width = 10, height = 20
      Square: width = 15, height = 15
      Square after change: width = 20, height = 20
    • 오브젝티브-C에서 상속, 다형성, 그리고 다른 객체 지향 프로그래밍의 기능들은 자바와 유사하다. 상위 클래스를 확장하려고 한다면, (단일 상속만 가능하다) 하위 클래스에서 상위 클래스에 있는 메소드와 같은 이름으로 메소드를 선언하고, 해당 내용을 구현을 함으로서 메소드 오버로딩을 할 수 있다. C++처럼 가상 테이블로 장난칠 필요가 없다.
    • 한 가지 짚고 넘어갈 문제가 있다. 만약에 다음과 같이 사각형 객체의 생성자를 출하면 어떻게 될것인가? Square *sq = [[Square alloc] initWithWidth: 10 height: 15]. 답은 이 문장은 컴파일 에러가 날것이다. rectangle 클래스 생성자가 반환하는 값이 Rectangle*이지 Square*가 아니기 때문에 이 문장은 에러가 나는 것이다. 이런 문제가 생길 경우에 id 변수를 사용하는 것이 좋다. 만약 하위 클래스에서 상위 클래스의 생성자를 사용하길 원한다면 Rectangle*의 반환 형을 id로 바꾸면 된다.

    동적 형

    • 오브젝티브-C에서는 동적형에서 사용할 수 있는 몇 가지 중요한 메소드가 있다.
      • -(BOOL) isKindOfClass: classObj 이 객체가 ClassObj의 멤버이거나 상속을 받은 하위클래스인지?
        -(BOOL) isMemberOfClass: classObj 이 객체가 classObj의 멤버인지
        -(BOOL) respondsToSelector: selector 이 객체가 selector란 이름의 메소드를 가지고 있는지?
        +(BOOL) instancesRespondToSelector: selector 이 클래스로 만들어진 객체가 지정된 셀렉터에 반응할 능력이 있는지?
        -(id) performSelector: selector 객체의 지정된 셀렉터를 실행한다.
    • NSObject로 부터 상속받은 모든 객체는 클래스 객체를 반환하는 메소드를 가지고 있다. 자바의 getClass() 메소드와 유사한데, 이 클래스 객체는 위의 메소드들을 사용할 수 있다.
    • 오브젝티브-C에서 셀렉터는 메시지를 표현하기 위해서 사용된다. 셀렉터를 만드는 문법은 다음 예를 보면 된다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • main.m
      1. #import "Square.h"  
      2. #import "Rectangle.h"  
      3. #import <stdio.h>  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];  
      7. Square *sq = [[Square alloc] initWithSize: 15];  
      8.   
      9. // isMemberOfClass  
      10.   
      11. // true  
      12. if ( [sq isMemberOfClass: [Square class]] == YES ) {  
      13. printf( "square is a member of square class\n" );  
      14. }  
      15.   
      16. // false  
      17. if ( [sq isMemberOfClass: [Rectangle class]] == YES ) {  
      18. printf( "square is a member of rectangle class\n" );  
      19. }  
      20.   
      21. // false  
      22. if ( [sq isMemberOfClass: [NSObject class]] == YES ) {  
      23. printf( "square is a member of object class\n" );  
      24. }  
      25.   
      26. // isKindOfClass  
      27.   
      28. // true  
      29. if ( [sq isKindOfClass: [Square class]] == YES ) {  
      30. printf( "square is a kind of square class\n" );  
      31. }  
      32.   
      33. // true  
      34. if ( [sq isKindOfClass: [Rectangle class]] == YES ) {  
      35. printf( "square is a kind of rectangle class\n" );  
      36. }  
      37.   
      38. // true  
      39. if ( [sq isKindOfClass: [NSObject class]] == YES ) {  
      40. printf( "square is a kind of object class\n" );  
      41. }  
      42.   
      43. // respondsToSelector  
      44.   
      45. // true  
      46. if ( [sq respondsToSelector: @selector( setSize: )] == YES ) {  
      47. printf( "square responds to setSize: method\n" );  
      48. }  
      49.   
      50. // false  
      51. if ( [sq respondsToSelector: @selector( nonExistant )] == YES ) {  
      52. printf( "square responds to nonExistant method\n" );  
      53. }  
      54.   
      55. // true  
      56. if ( [Square respondsToSelector: @selector( alloc )] == YES ) {  
      57. printf( "square class responds to alloc method\n" );  
      58. }  
      59.   
      60. // instancesRespondToSelector  
      61.   
      62. // false  
      63. if ( [Rectangle instancesRespondToSelector: @selector( setSize: )] == YES ) {  
      64. printf( "rectangle instance responds to setSize: method\n" );  
      65. }  
      66.   
      67. // true  
      68. if ( [Square instancesRespondToSelector: @selector( setSize: )] == YES ) {  
      69. printf( "square instance responds to setSize: method\n" );  
      70. }  
      71.   
      72. // free memory  
      73. [rec release];  
      74. [sq release];  
      75.   
      76. return 0;  
      77. }  
    • 결과
      square is a member of square class
      square is a kind of square class
      square is a kind of rectangle class
      square is a kind of object class
      square responds to setSize: method
      square class responds to alloc method
      square instance responds to setSize: method

    카테고리

    • 클래스에 메소드를 추가하고 싶을 때에는 보통 클래스를 확장시키면 된다. 이 방법이 여의치 않을 때가 있을 수 있는데 특히 소스코드가 없다던지 할 경우가 있을 것이다. 카테고리는 이미 존재하는 클래스를 확장시키지 않고 원하는 기능을 추가할 수 있도록 해준다. 유사한 기능이 루비(Ruby)에 있다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • FractionMath.h
      1. #import "Fraction.h"  
      2.   
      3. @interface Fraction (Math)  
      4. -(Fraction*) add: (Fraction*) f;  
      5. -(Fraction*) mul: (Fraction*) f;  
      6. -(Fraction*) div: (Fraction*) f;  
      7. -(Fraction*) sub: (Fraction*) f;  
      8. @end  
    • FractionMath.m
      1. #import "FractionMath.h"  
      2.   
      3. @implementation Fraction (Math)  
      4. -(Fraction*) add: (Fraction*) f {  
      5. return [[Fraction alloc] initWithNumerator: numerator * [f denominator] +  
      6. denominator * [f numerator]  
      7. denominator: denominator * [f denominator]];  
      8. }  
      9.   
      10. -(Fraction*) mul: (Fraction*) f {  
      11. return [[Fraction alloc] initWithNumerator: numerator * [f numerator]  
      12. denominator: denominator * [f denominator]];  
      13.   
      14. }  
      15.   
      16. -(Fraction*) div: (Fraction*) f {  
      17. return [[Fraction alloc] initWithNumerator: numerator * [f denominator]  
      18. denominator: denominator * [f numerator]];  
      19. }  
      20.   
      21. -(Fraction*) sub: (Fraction*) f {  
      22. return [[Fraction alloc] initWithNumerator: numerator * [f denominator] -  
      23. denominator * [f numerator]  
      24. denominator: denominator * [f denominator]];  
      25. }  
      26. @end  
    • main.m
      1. #import <stdio.h>  
      2. #import "Fraction.h"  
      3. #import "FractionMath.h"  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. // create a new instance  
      7. Fraction *frac1 = [[Fraction alloc] initWithNumerator: 1 denominator: 3];  
      8. Fraction *frac2 = [[Fraction alloc] initWithNumerator: 2 denominator: 5];  
      9. Fraction *frac3 = [frac1 mul: frac2];  
      10.   
      11. // print it  
      12. [frac1 print];  
      13. printf( " * " );  
      14. [frac2 print];  
      15. printf( " = " );  
      16. [frac3 print];  
      17. printf( "\n" );  
      18.   
      19. // free memory  
      20. [frac1 release];  
      21. [frac2 release];  
      22. [frac3 release];  
      23.   
      24. return 0;  
      25. }  
    • 결과
      1/3 * 2/5 = 2/15
    • 마법은 바로 2개의 @implementation과 @interface에 있다. @interface Fraction (Math) 과 @implementation Fraction (Math).
    • 같은 이름으로는 오직 한 개만의 카테고리가 존재한다. 다른 카테고리는 다른 유일한 이름으로 만들어야 한다.
    • 카테고리에는 인스턴스 변수를 추가할 수 없다.
    • 카테고리는 private 메소드를 만들 때 유용한다. 오브젝티브-C에는 자바에서 처럼 private/protected/public 메소드를 구분하는 명확한 개념이 없기 때문에, 감추고 싶은 메소드를 만들 때에는 카테고리를 이용해서 이러한 기능들을 숨길 수 있다. 이 방법은 클래스의 private 메소드들을 클래스 헤더파일(.h)로 빼서 소스파일(*.m)으로 옮기는 것이다. 다음이 방금 이야기 한 것의 간단한 예이다.
    • MyClass.h
      1. #import <Foundation/NSObject.h>  
      2.   
      3. @interface MyClass: NSObject  
      4. -(void) publicMethod;  
      5. @end  
    • MyClass.m
      1. #import "MyClass.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation MyClass  
      5. -(void) publicMethod {  
      6. printf( "public method\n" );  
      7. }  
      8. @end  
      9.   
      10. // private methods  
      11. @interface MyClass (Private)  
      12. -(void) privateMethod;  
      13. @end  
      14.   
      15. @implementation MyClass (Private)  
      16. -(void) privateMethod {  
      17. printf( "private method\n" );  
      18. }  
      19. @end  
    • main.m
      1. #import "MyClass.h"  
      2.   
      3. int main( int argc, const char *argv[] ) {  
      4. MyClass *obj = [[MyClass alloc] init];  
      5.   
      6. // this compiles  
      7. [obj publicMethod];  
      8.   
      9. // this throws errors when compiling  
      10. //[obj privateMethod];  
      11.   
      12. // free memory  
      13. [obj release];  
      14.   
      15. return 0;  
      16. }  
    • 결과
      public method

    포즈

    • 포즈는 카테고리와 유사하지만 좀 더 복잡하다. 포즈를 사용하면 클래스를 확장할 수도 있고, 하위 클래스를 포즈함으로써 상위 클래스를 전역적으로 바꿔치기 할 수 있다. 예를 들면, NSArray를 확장한 NSArrayChild 클래스를 만들었다고 하자. 만약 NSArrayChild 클래스를 NSArray를 포즈하도록 했다면, 당신의 코드 전체에서 자동으로 NSArray대신에 NSArrayChild를 사용할 것이다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • FractionB.h
      1. #import "Fraction.h"  
      2.   
      3. @interface FractionB: Fraction  
      4. -(void) print;  
      5. @end  
    • FractionB.m
      1. #import "FractionB.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation FractionB  
      5. -(void) print {  
      6. printf( "(%i/%i)", numerator, denominator );  
      7. }  
      8. @end  
    • main.m
      1. #import <stdio.h>  
      2. #import "Fraction.h"  
      3. #import "FractionB.h"  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];  
      7.   
      8. // print it  
      9. printf( "The fraction is: " );  
      10. [frac print];  
      11. printf( "\n" );  
      12.   
      13. // make FractionB pose as Fraction  
      14. [FractionB poseAsClass: [Fraction class]];  
      15.   
      16. Fraction *frac2 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];  
      17.   
      18. // print it  
      19. printf( "The fraction is: " );  
      20. [frac2 print];  
      21. printf( "\n" );  
      22.   
      23. // free memory  
      24. [frac release];  
      25. [frac2 release];  
      26.   
      27. return 0;  
      28. }  
    • 결과
      The fraction is: 3/10
      The fraction is: (3/10)
    • 첫번째 fraction에서 출력한 결과는 3/10이지만 두번째 출력은 (3/10)이다. 이 값은 FractionB로 부터 나왔다.
    • 메소드 poseAsClass는 NSObject의 일부분이다. 이 메소드가 하위 클래스를 상위 클래스인 것처럼 행사할 수 있게 해준다.

    프로토콜

    • 오브젝티브-C에서의 프로토콜은 자바의 인터페이스나 C++의 순수가상함수와 동일하다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Printing.h
      1. @protocol Printing  
      2. -(void) print;  
      3. @end  
    • Fraction.h
      1. #import <Foundation/NSObject.h>  
      2. #import "Printing.h"  
      3.   
      4. @interface Fraction: NSObject <Printing, NSCopying> {  
      5. int numerator;  
      6. int denominator;  
      7. }  
      8.   
      9. -(Fraction*) initWithNumerator: (int) n denominator: (int) d;  
      10. -(void) setNumerator: (int) d;  
      11. -(void) setDenominator: (int) d;  
      12. -(void) setNumerator: (int) n andDenominator: (int) d;  
      13. -(int) numerator;  
      14. -(int) denominator;  
      15. @end  
    • Fraction.m
      1. #import "Fraction.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation Fraction  
      5. -(Fraction*) initWithNumerator: (int) n denominator: (int) d {  
      6. self = [super init];  
      7.   
      8. if ( self ) {  
      9. [self setNumerator: n andDenominator: d];  
      10. }  
      11.   
      12. return self;  
      13. }  
      14.   
      15. -(void) print {  
      16. printf( "%i/%i", numerator, denominator );  
      17. }  
      18.   
      19. -(void) setNumerator: (int) n {  
      20. numerator = n;  
      21. }  
      22.   
      23. -(void) setDenominator: (int) d {  
      24. denominator = d;  
      25. }  
      26.   
      27. -(void) setNumerator: (int) n andDenominator: (int) d {  
      28. numerator = n;  
      29. denominator = d;  
      30. }  
      31.   
      32. -(int) denominator {  
      33. return denominator;  
      34. }  
      35.   
      36. -(int) numerator {  
      37. return numerator;  
      38. }  
      39.   
      40. -(Fraction*) copyWithZone: (NSZone*) zone {  
      41. return [[Fraction allocWithZone: zone] initWithNumerator: numerator  
      42. denominator: denominator];  
      43. }  
      44. @end  
    • Complex.h
      1. #import <Foundation/NSObject.h>  
      2. #import "Printing.h"  
      3.   
      4. @interface Complex: NSObject <Printing> {  
      5. double real;  
      6. double imaginary;  
      7. }  
      8.   
      9. -(Complex*) initWithReal: (double) r andImaginary: (double) i;  
      10. -(void) setReal: (double) r;  
      11. -(void) setImaginary: (double) i;  
      12. -(void) setReal: (double) r andImaginary: (double) i;  
      13. -(double) real;  
      14. -(double) imaginary;  
      15. @end  
    • Complex.m
      1. #import "Complex.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation Complex  
      5. -(Complex*) initWithReal: (double) r andImaginary: (double) i {  
      6. self = [super init];  
      7.   
      8. if ( self ) {  
      9. [self setReal: r andImaginary: i];  
      10. }  
      11.   
      12. return self;  
      13. }  
      14.   
      15. -(void) setReal: (double) r {  
      16. real = r;  
      17. }  
      18.   
      19. -(void) setImaginary: (double) i {  
      20. imaginary = i;  
      21. }  
      22.   
      23. -(void) setReal: (double) r andImaginary: (double) i {  
      24. real = r;  
      25. imaginary = i;  
      26. }  
      27.   
      28. -(double) real {  
      29. return real;  
      30. }  
      31.   
      32. -(double) imaginary {  
      33. return imaginary;  
      34. }  
      35.   
      36. -(void) print {  
      37. printf( "%_f + %_fi", real, imaginary );  
      38. }  
      39. @end  
    • main.m
      1. #import <stdio.h>  
      2. #import "Fraction.h"  
      3. #import "Complex.h"  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. // create a new instance  
      7. Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];  
      8. Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15];  
      9. id <Printing> printable;  
      10. id <NSCopying, Printing> copyPrintable;  
      11.   
      12. // print it  
      13. printable = frac;  
      14. printf( "The fraction is: " );  
      15. [printable print];  
      16. printf( "\n" );  
      17.   
      18. // print complex  
      19. printable = comp;  
      20. printf( "The complex number is: " );  
      21. [printable print];  
      22. printf( "\n" );  
      23.   
      24. // this compiles because Fraction comforms to both Printing and NSCopyable  
      25. copyPrintable = frac;  
      26.   
      27. // this doesn't compile because Complex only conforms to Printing  
      28. //copyPrintable = comp;  
      29.   
      30. // test conformance  
      31.   
      32. // true  
      33. if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) {  
      34. printf( "Fraction conforms to NSCopying\n" );  
      35. }  
      36.   
      37. // false  
      38. if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) {  
      39. printf( "Complex conforms to NSCopying\n" );  
      40. }  
      41.   
      42. // free memory  
      43. [frac release];  
      44. [comp release];  
      45.   
      46. return 0;  
      47. }  
    • 결과
      The fraction is: 3/10
      The complex number is: 5.000000 + 15.000000i
      Fraction conforms to NSCopying
    • 프로토콜 문법은 간단하다. @protocol ProtocolName (methods you must implement) @end 이라고 작성하면 된다
    • 프로토콜에 따르게 하기 위해서는 따르게 하고자 하는 프로토콜을 <> 안에 넣고 쉼표로 이 것들을 구분한다. 예: @interface SomeClass <Protocol1, Protocol2, Protocol3>
    • 프로토콜이 사용하기 위해서 구현되어야 하는 메소드들은 헤더파일에 메소드의 리스트에 있을 필요는 없다. 위에서 보는 바와 마찬가지로 Complex.h에는 -(void) print에 대한 정의가 없다. 하지만 protocol에 부합되기 위해서 구현이 되어야 한다.
    • 오브젝티브-C’의 인터페이스 시스템에서 한가지 특이한 점은 사용자가 어떻게 형을 지정하는가 하는 점이다. 자바나 C++에서라면 다음과 같이 Printing *someVar = ( Printing * ) frac; 명시적으로 형을 표현했을 것이지만, 오브젝티브-C에서는 id형과 프로토콜을 사용해서 다음과 같이 id <Printing> var = frac; 표현한다. 이것이 여러 개의 프로토콜에서 필요로 하는 형을 단 하나의 변수를 사용해서 동적으로 명시하도록 해준다. 다음의 예를 보라 id <Printing, NSCopying> var = frac;
    • 객체의 상속을 시험하기 위해 @selector를 사용하는 것과 마찬가지로, 인터페이스의 합치성을 시험하기 위해서 @protocol을 사용할 수 있다. [object conformsToProtocol: @protocol( SomeProtocol )]은 프로토콜과 객체가 규격을 준수하는지의 여부를 BOOL형으로 반환하는 함수이다. 클래스의 경우에도 마찬가지로 사용할 수 있다. [SomeClass conformsToProtocol: @protocol( SomeProtocol )].

메모리 관리

  • 지금까지 오브젝티브-C에서 메모리 관리하는 부분에 대해 자세히 설명을 하지 않았었다. 물론 객체는 dealloc하면 메모리가 해지되겠지만, 객체가 다른 객체로의 포인터를 가지고 있는 경우에는 어떻게 할 것인가? 이런 객체의 메모리를 해제할 때에는 주의를 기울여야 할 것이다. 도한 파운데이션 프레임워크에서는 클래스를 만들 때 어떻게 메모리를 관리하는 지에 대해서도 알아야 할 것이다. 이런 것에 대한 설명을 이제 시작하려고 한다.
  • 주의: 지금까지 작성한 예제들은 모두 적절히 메모리가 관리되었다. 혹시 궁금해할까바.
  • Retain과 Release

    • Retain과 release는 NSObject를 상속받은 모든 객체가 가지고 있는 2개의 메소드이다. 모든 객체는 내부적으로 카운터를 가지고 있어서 객체의 레퍼런스의 개수를 추적하고 있다. 그래서 만약 3개의 레퍼런스를 가지고 있는 경우에는 자기 자신을 dealloc하면 안될 것이다. 만약 0개의 레퍼런스를 가지고 있다면 dealloc해도 될 것이다. [object retain]은 레퍼런스 카운터를 하나 증가시키고 [object release]는 하나 감소시킨다. 만약 [object release]가 호출되어서 카운터가 0에 도달하면 dealloc이 호출된다.
    • Fraction.m
      1. ...  
      2. -(void) dealloc {  
      3. printf( "Deallocing fraction\n" );  
      4. [super dealloc];  
      5. }  
      6. ...  
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • main.m
      1. #import "Fraction.h"  
      2. #import <stdio.h>  
      3.   
      4. int main( int argc, const char *argv[] ) {  
      5. Fraction *frac1 = [[Fraction alloc] init];  
      6. Fraction *frac2 = [[Fraction alloc] init];  
      7.   
      8. // print current counts  
      9. printf( "Fraction 1 retain count: %i\n", [frac1 retainCount] );  
      10. printf( "Fraction 2 retain count: %i\n", [frac2 retainCount] );  
      11.   
      12. // increment them  
      13. [frac1 retain]; // 2  
      14. [frac1 retain]; // 3  
      15. [frac2 retain]; // 2  
      16.   
      17. // print current counts  
      18. printf( "Fraction 1 retain count: %i\n", [frac1 retainCount] );  
      19. printf( "Fraction 2 retain count: %i\n", [frac2 retainCount] );  
      20.   
      21. // decrement  
      22. [frac1 release]; // 2  
      23. [frac2 release]; // 1  
      24.   
      25. // print current counts  
      26. printf( "Fraction 1 retain count: %i\n", [frac1 retainCount] );  
      27. printf( "Fraction 2 retain count: %i\n", [frac2 retainCount] );  
      28.   
      29. // release them until they dealloc themselves  
      30. [frac1 release]; // 1  
      31. [frac1 release]; // 0  
      32. [frac2 release]; // 0  
      33. }  
    • 결과
      Fraction 1 retain count: 1
      Fraction 2 retain count: 1
      Fraction 1 retain count: 3
      Fraction 2 retain count: 2
      Fraction 1 retain count: 2
      Fraction 2 retain count: 1
      Deallocing fraction
      Deallocing fraction
    • retain 호출이 카운터를 증가시키고, release 호출이 카운터를 감소시킨다. 카운트 자체는 [obj retainCount]를 통해서 얻을 수 있다. 카운터가 0에 도달하면 두 객체 모두 dealloc이 실행되고 둘다 “Deallocing fraction”이란 메시지를 출력하는 것을 볼 수 있다.

    Dealloc

    • 객체가 다른 객체를 포함하고 있는 경우에는 자기 자신을 제거하기 전에 다른 객체도 메모리에서 제거시켜야 한다. 오브젝티브-C를 사용할 때의 다른 좋은 점은 nil에도 메시지를 전달할 수 있기 때문에 객체를 제거할 때 에러 검사를 많이 할 필요가 없다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • AddressCard.h
      1. #import <Foundation/NSObject.h>  
      2. #import <Foundation/NSString.h>  
      3.   
      4. @interface AddressCard: NSObject {  
      5. NSString *first;  
      6. NSString *last;  
      7. NSString *email;  
      8. }  
      9.   
      10. -(AddressCard*) initWithFirst: (NSString*) f  
      11. last: (NSString*) l  
      12. email: (NSString*) e;  
      13. -(NSString*) first;  
      14. -(NSString*) last;  
      15. -(NSString*) email;  
      16. -(void) setFirst: (NSString*) f;  
      17. -(void) setLast: (NSString*) l;  
      18. -(void) setEmail: (NSString*) e;  
      19. -(void) setFirst: (NSString*) f  
      20. last: (NSString*) l  
      21. email: (NSString*) e;  
      22. -(void) setFirst: (NSString*) f last: (NSString*) l;  
      23. -(void) print;  
      24. @end  
    • AddressCard.m
      1. #import "AddressCard.h"  
      2. #import <stdio.h>  
      3.   
      4. @implementation AddressCard  
      5. -(AddressCard*) initWithFirst: (NSString*) f  
      6. last: (NSString*) l  
      7. email: (NSString*) e {  
      8. self = [super init];  
      9.   
      10. if ( self ) {  
      11. [self setFirst: f last: l email: e];  
      12. }  
      13.   
      14. return self;  
      15. }  
      16.   
      17. -(NSString*) first {  
      18. return first;  
      19. }  
      20.   
      21. -(NSString*) last {  
      22. return last;  
      23. }  
      24.   
      25. -(NSString*) email {  
      26. return email;  
      27. }  
      28.   
      29. -(void) setFirst: (NSString*) f {  
      30. [f retain];  
      31. [first release];  
      32. first = f;  
      33. }  
      34.   
      35. -(void) setLast: (NSString*) l {  
      36. [l retain];  
      37. [last release];  
      38. last = l;  
      39. }  
      40.   
      41. -(void) setEmail: (NSString*) e {  
      42. [e retain];  
      43. [email release];  
      44. email = e;  
      45. }  
      46.   
      47. -(void) setFirst: (NSString*) f  
      48. last: (NSString*) l  
      49. email: (NSString*) e {  
      50. [self setFirst: f];  
      51. [self setLast: l];  
      52. [self setEmail: e];  
      53. }  
      54.   
      55. -(void) setFirst: (NSString*) f last: (NSString*) l {  
      56. [self setFirst: f];  
      57. [self setLast: l];  
      58. }  
      59.   
      60. -(void) print {  
      61. printf( "%s %s <%s>", [first cString],  
      62. [last cString],  
      63. [email cString] );  
      64. }  
      65.   
      66. -(void) dealloc {  
      67. [first release];  
      68. [last release];  
      69. [email release];  
      70.   
      71. [super dealloc];  
      72. }  
      73. @end  
    • main.m
      1. #import "AddressCard.h"  
      2. #import <Foundation/NSString.h>  
      3. #import <stdio.h>  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. NSString *first =[[NSString alloc] initWithCString: "Tom"];  
      7. NSString *last = [[NSString alloc] initWithCString: "Jones"];  
      8. NSString *email = [[NSString alloc] initWithCString: "tom@jones.com"];  
      9. AddressCard *tom = [[AddressCard alloc] initWithFirst: first  
      10. last: last  
      11. email: email];  
      12.   
      13. // we're done with the strings, so we must dealloc them  
      14. [first release];  
      15. [last release];  
      16. [email release];  
      17.   
      18. // print to show the retain count  
      19. printf( "Retain count: %i\n", [[tom first] retainCount] );  
      20. [tom print];  
      21. printf( "\n" );  
      22.   
      23. // free memory  
      24. [tom release];  
      25.   
      26. return 0;  
      27. }  
    • 결과
      Retain count: 1
      Tom Jones <tom@jones.com>
    • 이 예제의 AddressCard.m을 보면 어떻게 dealloc 메소드를 호출하는 지와 AddressCard 클래스의 멤버 변수들의 메모리를 해제할 때 어떻게 해야 하는 지를 알 수 있다.
    • 각각의 set 메소드에서 일어나는 3동작의 순서가 매우 중요하다. set 함수에 자신의 인자를 넘긴다고 해보자(좀 이상한 예이지만 이런 경우도 있다.) 만약 release를 먼저하고 그 다음에 retain을 한다면 자기 자신을 제거해 버릴 것이다. 따라서 항상 반드시 다음의 순서로 값을 설정하는 것이 중요하다. 1) retain 2) release 3) 값 정하기
    • 보통의 경우에 C 문자열로 변수를 초기하는 경우는 없을 것이다. 왜냐면 C 스트링은 유니코드를 지원하지 않기 때문이다. 다음의 예제에서는 NSAutoreleasePool을 사용하는데, 여기에서 문자열을 사용하고 초기화하는 적절한 방법을 다룰 것이다.
    • 이 방법은 멤버 변수 메모리 관리하는 한 방법에 불과하다. 또 다른 방법으로는 set 메소드 안에서 값을 복사하는 방법이 있다.

    Autorelease Pool

    • NSString과 다른 파운데이션 프레임워크를 이용해서 프로그래밍 할 때 더 유연한 시스템이 필요할 것이다. 이 시스템은 Autolease Pool을 사용한다.
    • 맥 코코아 어플리케이션을 사용할 때, auto release pool은 자동으로 사용 가능하다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • main.m
      1. #import <Foundation/NSString.h>  
      2. #import <Foundation/NSAutoreleasePool.h>  
      3. #import <stdio.h>  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
      7. NSString *str1 = @"constant string";  
      8. NSString *str2 = [NSString stringWithString: @"string managed by the pool"];  
      9. NSString *str3 = [[NSString alloc] initWithString: @"self managed string"];  
      10.   
      11. // print the strings  
      12. printf( "%s retain count: %x\n", [str1 cString], [str1 retainCount] );  
      13. printf( "%s retain count: %x\n", [str2 cString], [str2 retainCount] );  
      14. printf( "%s retain count: %x\n", [str3 cString], [str3 retainCount] );  
      15.   
      16. // free memory  
      17. [str3 release];  
      18.   
      19. // free pool  
      20. [pool release];  
      21. return 0;  
      22. }  
    • 결과
      constant string retain count: ffffffff
      string managed by the pool retain count: 1
      self managed string retain count: 1
    • 이 예제에서 몇 가지가 눈에 뛸 것이다. 먼저 str1의 카운터가 ffffff이다.
    • 다른 점은 str3만 release 했는데, 이 프로그램에서 메모리 관리는 완벽히 이뤄지고 있다. 그 이유는 첫번째 상수 문자열은 자동으로 autorelease pool에 추가되었고, 다른 문자열은 stringWithString으로 만들어 졌는데, 이 함수는 NSString 클래스에 포함된 문자열을 만들면서 자체적으로 auto release pool에 문자열을 추가한다.
    • 적절한 메모리 관리를 위해서 반드시 기억해야 할 점은, 편의성을 위해 만든 [NSString stringWithString: @"String"]와 같은 함수들은 autorelease pool을 사용한다는 점이다. 반면 [[NSString alloc] initWithString: @”String”]과 같이 직접 메모리를 할당해서 만든 함수들은 메모리 관리를 위해 autorelease pools을 사용하지 않는다는 점이다.
    • 오브젝티브-C에서는 메모리를 관리하는데 2가지 방법이 있다. 1) retain과 release 혹은 2) retain과 release/autorelease.
    • 모든 retain에는 반드시 한 개의 release나 autorealse가 있어야 한다.
    • 다음 예제가 이 내용을 설명하고 있다.
    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • Fraction.h
      1. ...  
      2. +(Fraction*) fractionWithNumerator: (int) n denominator: (int) d;  
      3. ...  
    • Fraction.m
      1. ...  
      2. +(Fraction*) fractionWithNumerator: (int) n denominator: (int) d {  
      3. Fraction *ret = [[Fraction alloc] initWithNumerator: n denominator: d];  
      4. [ret autorelease];  
      5.   
      6. return ret;  
      7. }  
      8. ...  
    • main.m
      1. #import <Foundation/NSAutoreleasePool.h>  
      2. #import "Fraction.h"  
      3. #import <stdio.h>  
      4.   
      5. int main( int argc, const char *argv[] ) {  
      6. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
      7. Fraction *frac1 = [Fraction fractionWithNumerator: 2 denominator: 5];  
      8. Fraction *frac2 = [Fraction fractionWithNumerator: 1 denominator: 3];  
      9.   
      10. // print frac 1  
      11. printf( "Fraction 1: " );  
      12. [frac1 print];  
      13. printf( "\n" );  
      14.   
      15. // print frac 2  
      16. printf( "Fraction 2: " );  
      17. [frac2 print];  
      18. printf( "\n" );  
      19.   
      20. // this causes a segmentation fault  
      21. //[frac1 release];  
      22.   
      23. // release the pool and all objects in it  
      24. [pool release];  
      25. return 0;  
      26. }  
    • 결과
      Fraction 1: 2/5
      Fraction 2: 1/3
    • 이 예제에서, 모든 메소드는 클래스 레벨 메소드 이다. 객체가 생성된 후에, autorelease가 호출되었다. main 블럭 안에서 객체에 대한 release는 부르지 않았다.
    • 이 예제가 작동하는 원리는 다음과 같다. 매번 retain이 일어났을 경우에는 반드시 release나 autorelease가 불려져야 한다. 객체의 카운터는 1부터 시작한다. 그리고 autorelease를 부르면 1 – 1 = 0 즉 메모리가 해제되게 된다. 따라서 autorelease pool이 해제되면, 모든 객체의 autorelease 호출된 횟수를 세서 [obj release]를 이용해서 객체당 autorelease가 불려진 횟수와 같은 숫자만큼 카운트를 줄여나간다.
    • 위의 주석에 있는 것처럼, 주석 처리된 줄을 주석 해제 시키면 세그먼트 오류가 날 것이다. autorelease가 이미 객체에서 불려졌기 때문에, release를 호출하고, autorelease pool에서 다시 release를 호출하면 이미 nil이 된 객체에서 다시 dealloc을 호출하는 것이 될 테니 올바르지 않다. 수식으로 다시 정리해보면 1(생성) – 1(release) – 1(autorelease) = -1이 되는 것이다.
    • Auto release pools은 동적으로 많은 양의 임시 객체들을 만들어 낼 수 있다. 이 때의 올바른 절차는 먼저 풀을 만들고, 많은 임시 객체를 만드는 코드를 실행을 시킨 다음에 풀을 제거하는 것이다. 혹시 궁금할 경우에 대비해서 이야기 하면, 한번에 1개 이상의 auto release pool을 갖는 것도 가능하다.

Foundation framework 클래스

  • Foundation framework은 C++의 Standard Template Library와 유사하다. 오브젝티브-C에는 진짜 동적 형이 있어서, C++의 탬플릿처럼 조잡하게 코딩하지 않아도 된다. 이 프레임워크에는 자료 구조, 네트워킹, 쓰레드 외 여러가지 것들이 포함되어 있다.
  • NSArray

    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • main.m
      1. #import <Foundation/NSArray.h>  
      2. #import <Foundation/NSString.h>  
      3. #import <Foundation/NSAutoreleasePool.h>  
      4. #import <Foundation/NSEnumerator.h>  
      5. #import <stdio.h>  
      6.   
      7. void print( NSArray *array ) {  
      8. NSEnumerator *enumerator = [array objectEnumerator];  
      9. id obj;  
      10.   
      11. while ( obj = [enumerator nextObject] ) {  
      12. printf( "%s\n", [[obj description] cString] );  
      13. }  
      14. }  
      15.   
      16. int main( int argc, const char *argv[] ) {  
      17. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
      18. NSArray *arr = [[NSArray alloc] initWithObjects:  
      19. @"Me", @"Myself", @"I", nil];  
      20. NSMutableArray *mutable = [[NSMutableArray alloc] init];  
      21.   
      22. // enumerate over items  
      23. printf( "----static array\n" );  
      24. print( arr );  
      25.   
      26. // add stuff  
      27. [mutable addObject: @"One"];  
      28. [mutable addObject: @"Two"];  
      29. [mutable addObjectsFromArray: arr];  
      30. [mutable addObject: @"Three"];  
      31.   
      32. // print em  
      33. printf( "----mutable array\n" );  
      34. print( mutable );  
      35.   
      36. // sort then print  
      37. printf( "----sorted mutable array\n" );  
      38. [mutable sortUsingSelector: @selector( caseInsensitiveCompare: )];  
      39. print( mutable );  
      40.   
      41. // free memory  
      42. [arr release];  
      43. [mutable release];  
      44. [pool release];  
      45.   
      46. return 0;  
      47. }  
    • 결과
      ----static array
      Me
      Myself
      I
      ----mutable array
      One
      Two
      Me
      Myself
      I
      Three
      ----sorted mutable array
      I
      Me
      Myself
      One
      Three
      Two
    • NSArray와 NSMutableArray의 두 가지 종류의 배열이 있다. (파운데이션 클래스에서 가장 데이터에 중점을 둔). 이름에서도 알 수 있듯이 Mutable은 교환 가능하고 NSArray는 그렇지 않다. 다시 말해 NSArray를 만들 수는 있지만, 한번 만든 후에는 길이를 변화시킬 수 없다는 뜻이다.
    • 배열은 생성자에 Obj, Obj, Obj, …, nil 이런 형태로 배열을 초기화 시킬 수도 있다. 여기에서 nil은 끝을 표시해준다.
    • 정렬 부분은 셀렉터(selector)를 사용해서 객체를 정렬하는 법을 보여준다. 셀러터는 배열을 NSString의 대소문자를 구분하지 않는 비교를 사용하라고 지시해 주고 있다. 만약 객체가 여러가지 정렬 방법을 있다면, 사용자가 셀렉터를 이용해서 자신이 원하는 정렬방법을 지정해 줘야 한다.
    • print 메소드에서, 메소드 표시를 사용했다. 이것은 자바의 toString이랑 유사한다. 객체를 NSString으로 표시한 것을 반환한다.
    • NSEnumerator은 자바의 열거형 시스템이랑 비슷하다. while ( obj = [array objectEnumerator] )이 작동하는 원리는 obj가 가장 마지막 객체에 들어가면 nil이 반환되기 때문이다. 보통 C에서는 nil은 0이고, 곧 false이다. 보다 정확하게 하려면 ( ( obj = [array objectEnumerator] ) != nil ) 이 나을지도 모르겠다.

    NSDictionary

    • 예제의 출처: “Programming in 오브젝티브-C,” Copyright © 2004 출판사 Sams Publishing. 허가 받고 사용함
    • main.m
      1. #import <Foundation/NSString.h>  
      2. #import <Foundation/NSAutoreleasePool.h>  
      3. #import <Foundation/NSDictionary.h>  
      4. #import <Foundation/NSEnumerator.h>  
      5. #import <Foundation/Foundation.h<  
      6. #import <stdio.h>  
      7.   
      8. void print( NSDictionary *map ) {  
      9. NSEnumerator *enumerator = [map keyEnumerator];  
      10. id key;  
      11.   
      12. while ( key = [enumerator nextObject] ) {  
      13. printf( "%s => %s\n",  
      14. [[key description] cString],  
      15. [[[map objectForKey: key] description] cString] );  
      16. }  
      17. }  
      18.   
      19. int main( int argc, const char *argv[] ) {  
      20. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
      21. NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:  
      22. @"one", [NSNumber numberWithInt: 1],  
      23. @"two", [NSNumber numberWithInt: 2],  
      24. @"three", [NSNumber numberWithInt: 3],  
      25. nil];  
      26. NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init];  
      27.   
      28. // print dictionary  
      29. printf( "----static dictionary\n" );  
      30. print( dictionary );  
      31.   
      32. // add objects  
      33. [mutable setObject: @"Tom" forKey: @"tom@jones.com"];  
      34. [mutable setObject: @"Bob" forKey: @"bob@dole.com" ];  
      35.   
      36. // print mutable dictionary  
      37. printf( "----mutable dictionary\n" );  
      38. print( mutable );  
      39.   
      40. // free memory  
      41. [dictionary release];  
      42. [mutable release];  
      43. [pool release];  
      44.   
      45. return 0;  
      46. }  
    • 결과
      ----static dictionary
      1 => one
      2 => two
      3 => three
      ----mutable dictionary
      bob@dole.com => Bob
      tom@jones.com => Tom

장단점

    장점

    • 카테고리
    • 포즈
    • 동적형
    • 포인터 카운팅
    • 유연한 메시지 전달
    • C로부터 적당한 정도로 확장을 해서 복잡하지 않음
    • 오브젝티브-C++을 통해서 C++을 사용할 수 있음

    단점

    • 네임스페이스가 없음
    • 연산자 오버로딩이 없음(이 점은 장점이라고 할 수 도 있지만, 연산자 오버로딩을  쓰면, 코드의 부자연스러운 점을 줄일 수 있다.)
    • 언어 자체적으로 세련되지 못한 점이 있으나, C++이랑 비슷한 수준

추가 정보


최종 수정: April 13, 2004.

한글 번역: September 11, 2010, by Michael Kang

반응형

+ Recent posts