NSLog NSDictionary Example code


 

NSLog NSDictionary Example code

In this example code we will learn how to print the value of NSDictionary

In this example code we will learn how to print the value of NSDictionary

The following code example prints the data of NSDictionary.

You can use the NSLog function to debug your program. In this example we will be showing you the code that prints the value of NSDictionary object.

//
//  HelloDictionary.h
//  DataTypes
//

#import <Foundation/Foundation.h>

@interface HelloDictionary : NSObject {

    NSDictionary *dictionary;

}

-(void) print;

@end

The implementation class is as follows:

//
//  HelloDictionary.m
//  DataTypes
//

#import "HelloDictionary.h"

@implementation HelloDictionary

-(void) print

{

    NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", @"key3", nil];

    NSArray *objects = [NSArray arrayWithObjects:@"How", @"are", @"you", nil];

    dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

    for (id key in dictionary) {

        NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);

    }

}

@end

If you run the above code the following output will be shown on the console.

2009-07-29 15:42:34.634 DataTypes[560:10b] key: key1, value: How

2009-07-29 15:42:34.636 DataTypes[560:10b] key: key2, value: are

2009-07-29 15:42:34.639 DataTypes[560:10b] key: key3, value: you

 

Ads