How to update NSMutableDictionary?
Asked Answered
B

3

9

I have an NSMutableDictionary.

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

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

Basir answered 9/2, 2010 at 7:16 Comment(2)
depends what you mean by update...Villiers
i have to remove an object and insert another value for a key.This NSMutableDictionary is a plist of application settings of user. User will change it frequently.Basir
G
12

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
Grandeur answered 9/2, 2010 at 7:19 Comment(3)
thanks. i tried like this. it worked.. [plistDict removeObjectForKey:@"name"] ; [plistDict setObject:@"my name" forKey:@"name"];Basir
You don't need to remove the object for a key before setting the new value.Palpable
i need only [plistDict setObject:@"my name" forKey:@"name"]; ? ok. i will try this .Thanks.Basir
M
2

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

Marcasite answered 9/11, 2012 at 14:31 Comment(0)
E
2

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.

Elmore answered 26/1, 2013 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.