I am developing an application in which I want to use an NSDictionary
. Can anyone please send me a sample code explaining the procedure how to use an NSDictionary
to store Data with a perfect example?
Main Difference is:NSMutableDictionary is derived from NSDictionary, it has all the methods of NSDictionary. NSMutableDictionary is mutable( can be modified) but NSDictionary is immutable (can not be modified).
NSDictionary / NSMutableDictionary copies keys, and holds strong references to values. NSMapTable is mutable, without an immutable counterpart. NSMapTable can hold keys and values with weak references, in such a way that entries are removed when either the key or value is deallocated.
Use -mutableCopy . NSDictionary *d; NSMutableDictionary *m = [d mutableCopy]; Note that -mutableCopy returns id ( Any in Swift) so you will want to assign / cast to the right type. It creates a shallow copy of the original dictionary.
NSDictionary creates static dictionaries, and NSMutableDictionary creates dynamic dictionaries. (For convenience, the term dictionary refers to any instance of one of these classes without specifying its exact class membership.) A key-value pair within a dictionary is called an entry.
The NSDictionary and NSMutableDictionary docs are probably your best bet. They even have some great examples on how to do various things, like...
...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 mutable
NSMutableDictionary *mutableDict = [dictionary mutableCopy];
Note: historic version before 2010: [[dictionary mutableCopy] autorelease]
...and alter it
[mutableDict setObject:@"value3" forKey:@"key3"];
...then store it to a file
[mutableDict writeToFile:@"path/to/file" atomically:YES];
...and read it back again
NSMutableDictionary *anotherDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"path/to/file"];
...read a value
NSString *x = [anotherDict objectForKey:@"key1"];
...check if a key exists
if ( [anotherDict objectForKey:@"key999"] == nil ) NSLog(@"that key is not there");
...use scary futuristic syntax
From 2014 you can actually just type dict[@"key"] rather than [dict objectForKey:@"key"]
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With