Skip to content

Instantly share code, notes, and snippets.

@yetanotherchris
Created February 9, 2013 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yetanotherchris/4747055 to your computer and use it in GitHub Desktop.
Save yetanotherchris/4747055 to your computer and use it in GitHub Desktop.
C# to Objective C by example #7
-(void) dictionaryExample
{
// 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]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment