How can we store in NSDictionary? What is the difference between NSDictionary and NSMutableDictionary?

I am developing an application in which I want to use NSDictionary . Can someone send me some sample code explaining the procedure for using NSDictionary to store data with a great example?

+66
objective-c iphone nsmutabledictionary nsdictionary
Nov 19 '09 at 1:34
source share
3 answers

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"]

+188
Nov 19 '09 at 1:40
source share
 NSDictionary *dict = [NSDictionary dictionaryWithObject: @"String" forKey: @"Test"]; NSMutableDictionary *anotherDict = [NSMutableDictionary dictionary]; [anotherDict setObject: dict forKey: "sub-dictionary-key"]; [anotherDict setObject: @"Another String" forKey: @"another test"]; NSLog(@"Dictionary: %@, Mutable Dictionary: %@", dict, anotherDict); // now we can save these to a file NSString *savePath = [@"~/Documents/Saved.data" stringByExpandingTildeInPath]; [anotherDict writeToFile: savePath atomically: YES]; //and restore them NSMutableDictionary *restored = [NSDictionary dictionaryWithContentsOfFile: savePath]; 
+32
Nov 19 '09 at 1:39
source share

Key difference: NSMutableDictionary can be changed in place, NSDictionary cannot . This is true for all other NSMutable * classes in Cocoa. NSMutableDictionary is a subclass of NSDictionary, so anything you can do with NSDictionary you can do with both. However, NSMutableDictionary also adds additional methods for modifying existing functions, such as the setObject:forKey: method.

You can convert between these two ways:

 NSMutableDictionary *mutable = [[dict mutableCopy] autorelease]; NSDictionary *dict = [[mutable copy] autorelease]; 

Presumably you want to save the data by writing it to a file. NSDictionary has a way to do this (which also works with NSMutableDictionary):

 BOOL success = [dict writeToFile:@"/file/path" atomically:YES]; 

There is a corresponding method for reading a dictionary from a file:

 NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/file/path"]; 

If you want to read the file as NSMutableDictionary, just use:

 NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/file/path"]; 
+18
Nov 19 '09 at 1:41
source share



All Articles