How to iterate over dictionary keys and values in Objective-C

1 Answer

0 votes
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
                            [NSNumber numberWithInt:8], @"c",
                            [NSNumber numberWithInt:5], @"objective c",
                            [NSNumber numberWithInt:3], @"c++",
                            [NSNumber numberWithInt:6], @"python",
                            [NSNumber numberWithInt:2], @"java", nil];
                            
    for(id key in dict)
        NSLog(@"key:%@ value:%@", key, [dict objectForKey:key]);


    [pool drain];
    
    return 0;
}




/*
run:

key:python value:6
key:objective c value:5
key:java value:2
key:c value:8
key:c++ value:3

*/

 



answered Feb 3, 2023 by avibootz

Related questions

1 answer 137 views
2 answers 171 views
1 answer 153 views
2 answers 202 views
...