Created
February 9, 2013 20:48
-
-
Save yetanotherchris/4747055 to your computer and use it in GitHub Desktop.
C# to Objective C by example #7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(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