アットウィキロゴ

高速列挙ループ

#import <Foundation/Foundation.h>

int main(void) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    // NSArrayをループする
    [[NSArray]] *array = [NSArray arrayWithObjects:@"aaa", @"bbb", @"ccc", @"ddd", nil];
   for (id obj in array) {
       NSLog(@"value: %@\n", obj);
   }

    // NSDictionaryをループする
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                         @"hoge", @"name",
                         [NSNumber numberWithInt:4], @"number",
                         @"blue", @"color",
                         nil];
   for (id key in dict) {
       NSLog(@"key: %@, value: %@\n", key, [dict objectForKey:key]);
   }

    // NSEnumeratorをループする
    NSEnumerator *enumerator = [dict objectEnumerator];
   for (id obj in enumerator) {
       NSLog(@"value: %@\n", obj);
   }

[pool drain];
return 0;
}
最終更新:2009年05月09日 17:30
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。