Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update NSMutableDictionary?

I have an NSMutableDictionary.

NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

I have to update an element in that dictionary. How i can do that?

like image 376
S.P. Avatar asked Feb 09 '10 07:02

S.P.


People also ask

What is NSMutableDictionary in Swift?

The NSMutableDictionary class declares the programmatic interface to objects that manage mutable associations of keys and values. It adds modification operations to the basic operations it inherits from NSDictionary . NSMutableDictionary is “toll-free bridged” with its Core Foundation counterpart, CFMutableDictionary .

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.


3 Answers

Please refer to the API here

First retrieve the objects using

[dict objectForKey:@"key"];

Later, you can set them back using:

- (void)setObject:(id)anObject forKey:(id)aKey
- (void)setValue:(id)value forKey:(NSString *)key
like image 155
Mihir Mathuria Avatar answered Nov 03 '22 00:11

Mihir Mathuria


dictionary only allows you to have one objects for each key so if you use "set object for key" it will update the current one

like image 41
pedrotorres Avatar answered Nov 03 '22 00:11

pedrotorres


NSMutableDictionary's method addEntriesFromDictionary.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html

If both dictionaries contain the same key, the receiving dictionary’s previous value object for that key is sent a release message, and the new value object takes its place.

like image 43
Renetik Avatar answered Nov 03 '22 00:11

Renetik