NSDictionary and NSMutableDictionary docs are probably your best bet. They even have great examples of how to do different things, for example ...
... create an NSDictionary
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil]; NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
... iterate over it
for (id key in dictionary) { NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); }
... make it volatile
NSMutableDictionary *mutableDict = [dictionary mutableCopy];
Note: historical version until 2010: [[mutableCopy dictionary] autorelease]
... and change it
[mutableDict setObject:@"value3" forKey:@"key3"];
... then save it to a file
[mutableDict writeToFile:@"path/to/file" atomically:YES];
... and read it again
NSMutableDictionary *anotherDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"path/to/file"];
... read the meaning
NSString *x = [anotherDict objectForKey:@"key1"];
... check if the key exists
if ( [anotherDict objectForKey:@"key999"] == nil ) NSLog(@"that key is not there");
... use scary futuristic syntax
Since 2014, you can simply type dict [@ "key"] rather than [dict objectForKey: @ "key"]
Tim Nov 19 '09 at 1:40 2009-11-19 01:40
source share