Display each item in an NSDictionary

I am trying to display each item in NSDictionary. I have tried using a for loop but that didn't work.


Try this code

for(NSString *key in [dict allKeys]) {
  NSLog(@"%@",[dict objectForKey:key]);

What about

NSLog(@"Dictionary: %@", [myDictionary description]);

Seems to work for me...

And if you want to save it on NSString you can use

NSMutableString *stringUserInfo = [[NSMutableString alloc] init];
for (NSString *aKey in dictionary.allKeys)
    [stringUserInfo appendFormat:@"%@ : %@\n",aKey,[dictionary valueForKey:aKey]];

*Try this simple example:

//.h file

@interface DictionaryClass : NSObject
    NSDictionary *dict;

-(void) intialiseDictionary;

-(void) displayDictonary;

//.m file
#import "DictionaryClass.h"

@implementation DictionaryClass

-(void) intialiseDictionary


    dict = @{@"key1":@"object1",@"key2":@"object2",@"key3":@"object3",@"key4":@"object4"};


-(void) displayDictonary

    for(NSString *keys in dict)
        NSLog(@"\n Dictionary object = %@",[dict objectForKey:keys]);




//main function

#import <Foundation/Foundation.h>
#import "DictionaryClass.h"

int main(int argc, const char * argv[])


        DictionaryClass *obj = [[DictionaryClass alloc]init];

        [obj intialiseDictionary];
        [obj displayDictonary];
    return 0;

This works for me and is very useful for debugging.

NSDictionary *jsonDictionary = [theJSON JSONValue];

NSLog(@"dictionary data %@",jsonDictionary);

i think you can print NSDictionary using

NSDictionary *dic;

NSLog (@"nsdic = %@", dic);

Hope its help.

Need Your Help