[Objective C] 15 NSDictionary

프로그래밍/iPhone Dev 2009. 11. 13. 15:44 Posted by galad
NSDictionary
java의 map.
NSMutableDictionary 도 소트는 안되는 듯.

main.m
#import <Foundation/NSString.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSEnumerator.h>
#import <Foundation/Foundation.h>
#import <stdio.h>

void print( NSDictionary *map ) {
    NSEnumerator *enumerator = [map keyEnumerator];
    id key;

    while ( key = [enumerator nextObject] ) {
        printf( "%s => %s\n",
                [[key description] cString],
                [[[map objectForKey: key] description] cString] );
    }
}

int main( int argc, const char *argv[] ) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
        @"one", [NSNumber numberWithInt: 1],
        @"two", [NSNumber numberWithInt: 2],
        @"three", [NSNumber numberWithInt: 3],
        nil];
    NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init];

    // print dictionary
    printf( "----static dictionary\n" );
    print( dictionary );

    // add objects
    [mutable setObject: @"Tom" forKey: @"tom@jones.com"];
    [mutable setObject: @"Bob" forKey: @"bob@dole.com" ];

    // print mutable dictionary
    printf( "----mutable dictionary\n" );
    print( mutable );

    // free memory
    [dictionary release];
    [mutable release];
    [pool release];

    system("PAUSE");
    return 0;
}


결과
----static dictionary
1 => one
3 => three
2 => two
----mutable dictionary
bob@dole.com => Bob
tom@jones.com => Tom
계속하려면 아무 키나 누르십시오 . . .

- Foundation Framework classes의 references만 찾아봐도 양이 상당할 듯...ㅎㄷㄷ

'프로그래밍 > iPhone Dev' 카테고리의 다른 글

[Objective C] Root Class  (0) 2009.11.20
[Objective C] Category  (0) 2009.11.18
[Objective C] 14 NSArray  (0) 2009.11.13
[Objective C] 13 Autorelease Pool  (1) 2009.11.13
[Objective C] 12 Dealloc  (1) 2009.11.12