Return to Snippet

Revision: 29978
at August 5, 2010 22:24 by marcuswyatt


Initial Code
// NSDictionary exists, but you can`t add/remove items to it once it`s initialised
// NSMutableDictionary inherits from NSDictionary
// More info: http://developer.apple.com/documentation/Cocoa/Conceptual/Collections/Collections.html
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

// This is another way to initialise the dictionary, remember to use nil at the end
// nil is just another definition of NULL
NSMutableDictionary *dictionary2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"obj1",@"key1",@"obj2",@"key2",nil];

// Adding keys
[dictionary setObject: @"my object" forKey:@"key"];
[dictionary setObject: @"my object" forKey:@"key2"];

// Getting by a single key
NSLog(@"%@", [dictionary objectForKey:@"key"]);

// Enumerating
for (NSString *key in dictionary)
	NSLog(@"key: %@, value: %@", key,[dictionary objectForKey:key]);

// Lookups would be performed using enumeration, there is no IndexOf or equivalent.

// Deleting
[dictionary removeObjectForKey:@"key"];

// Count comes from the base, NSDictionary
NSLog(@"Dictionary count: %i", dictionary.count);

Initial URL


Initial Description


Initial Title
Collections: NSDictionary

Initial Tags


Initial Language
Objective C