Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 329
Pradeep Reddy Kypa Avatar asked Nov 19 '09 01:11

Pradeep Reddy Kypa


People also ask

What is the difference between NSDictionary and NSMutableDictionary?

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).

What is the difference between NSMapTable vs NSDictionary?

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.

How do you convert NSDictionary to NSMutableDictionary?

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.

What is NSMutableDictionary?

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.


2 Answers

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

like image 92
Tim Avatar answered Sep 21 '22 22:09

Tim


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]; 
like image 37
Ben Gottlieb Avatar answered Sep 21 '22 22:09

Ben Gottlieb