Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
May 5, 2016
arrowPress Releases
May 5, 2016
PR Newswire
View All
View All     Submit Event






If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 
How to work with NSDictionary in Objective-C ?
by willie shi on 02/24/14 01:45:00 pm

The following blog post, unless otherwise noted, was written by a member of Gamasutraís community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

This tutorial will address few issues.
1. How to create dictionary by using Array.
2. Add records to dictionary by using KVC.
3. Retrieving value from NSDictionary.

Using method bellow to create dictionary.

-(void)createDict{

// ### creating Key array
NSArray *models = @[@"Mercedes-Benz", @"Ford",
@"GM", @"chrysler"];

// ### creating Value array
NSArray *stock = @[[NSNumber numberWithInt:10],
[NSNumber numberWithInt:20],
[NSNumber numberWithInt:30],
[NSNumber numberWithInt:40]];
// ### creating a dictionary base on array
NSDictionary *inventory = [NSDictionary dictionaryWithObjects:stock forKeys:models];
// ### creating a mutableDictionary in order to deploy key value coding (KVC)
NSMutableDictionary *mDict = [inventory mutableCopy];
// ### add record to existing dictionary
[mDict setValue:[NSNumber numberWithInt:100] forKey:@"Honda"];
// ### add NSString as a value to existing dictionary
[mDict setValue:@"123 in stock" forKey:@"Toyata"];

// ### print out original dictionary
NSLog(@"%@", inventory);
// #### output =>
/*
Ford = 20;
GM = 30;
"Mercedes-Benz" = 10;
chrysler = 40;
*/

// ### print out the Value for Key "chrysler" stored in dictionay
NSLog(@"chrysler has %@ in stock", inventory[@"chrysler"]);
// #### output => chrysler has 40 in stock

// ### print out the Value for Key "Ford" stored in dictionay
NSLog(@"Ford has %@ in stock", [inventory objectForKey:@"Ford"]);
// #### output => Ford has 20 in stock

// ### print out all Value from exsiting dictionary
NSLog(@"all value %@", [inventory allValues]);
// #### output => 20, 30, 40, 10

// ### print out formatted all Value from exsiting dictionary
for (NSString *str in [inventory allValues]) {
NSLog(@"All value: %@", str);
// #### output =>
/*
All value: 20
All value: 30
All value: 40
All value: 10
*/
}

// ### print out formatted all Value from mutableDictionary
for (NSString *str in [mDict allValues]) {
NSLog(@"mDict value: %@", str);
// ### output = >
/*
mDict value: 20
mDict value: 30
mDict value: 40
mDict value: 10
mDict value: 100
mDict value: 123 in stock
*/
}

// ### storing the value for Key "Toyata" in NSString
NSString *dValue = [mDict valueForKey:@"Toyata"];

// ### retrieving the doubleValue for Key "Toyata" stored in mutableDictionary
// ### cut the string "in stock", only pront out the number in double format.
NSLog(@"dValue: %.2f", [dValue doubleValue]);
// ### output = > dValue: 123.00
}


Related Jobs

Insomniac Games
Insomniac Games — Burbank, California, United States
[05.04.16]

Flash Developer
Insomniac Games
Insomniac Games — Burbank , California, United States
[05.04.16]

Senior Engine Programmer
Insomniac Games
Insomniac Games — Burbank , California, United States
[05.04.16]

Gameplay Programmer
Insomniac Games
Insomniac Games — Burbank , California, United States
[05.04.16]

Engine Programmer





Loading Comments

loader image