Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html" rel="noreferrer">NSDictionary</a> and <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html#//apple_ref/occ/cl/NSMutableDictionary" rel="noreferrer">NSMutableDictionary</a> docs are probably your best bet. They even have some great examples on how to do various things, like...</p> <p><strong>...create an NSDictionary</strong></p> <pre><code>NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil]; NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];</code></pre> <p><strong>...iterate over it</strong></p> <pre><code>for (id key in dictionary) { NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]); }</code></pre> <p><strong>...make it mutable</strong></p> <pre><code>NSMutableDictionary *mutableDict = [dictionary mutableCopy];</code></pre> <p><em>Note: historic version before 2010: [[dictionary mutableCopy] autorelease]</em></p> <p><strong>...and alter it</strong></p> <pre><code>[mutableDict setObject:@"value3" forKey:@"key3"];</code></pre> <p><strong>...then store it to a file</strong></p> <pre><code>[mutableDict writeToFile:@"path/to/file" atomically:YES];</code></pre> <p><strong>...and read it back again</strong></p> <pre><code>NSMutableDictionary *anotherDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"path/to/file"];</code></pre> <p><strong>...read a value</strong></p> <pre><code>NSString *x = [anotherDict objectForKey:@"key1"]; </code></pre> <p><strong>...check if a key exists</strong></p> <pre><code>if ( [anotherDict objectForKey:@"key999"] == nil ) NSLog(@"that key is not there"); </code></pre> <p><strong>...use scary futuristic syntax</strong></p> <p><em>From 2014 you can actually just type dict[@"key"] rather than [dict objectForKey:@"key"]</em></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload