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?

djot
  • 2,952
  • 4
  • 19
  • 28
S.P.
  • 5,427
  • 11
  • 56
  • 83
  • 1
    depends what you mean by update... – echo Feb 09 '10 at 07:19
  • 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. – S.P. Feb 09 '10 at 07:22

3 Answers3

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
Mihir Mathuria
  • 6,479
  • 1
  • 22
  • 15
  • thanks. i tried like this. it worked.. [plistDict removeObjectForKey:@"name"] ; [plistDict setObject:@"my name" forKey:@"name"]; – S.P. Feb 09 '10 at 07:34
  • 1
    You don't need to remove the object for a key before setting the new value. – rpetrich Feb 09 '10 at 08:35
  • i need only [plistDict setObject:@"my name" forKey:@"name"]; ? ok. i will try this .Thanks. – S.P. Feb 09 '10 at 08:48
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

pedrotorres
  • 1,222
  • 2
  • 13
  • 26
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.

MRizwan33
  • 2,723
  • 6
  • 31
  • 42
Renetik
  • 5,887
  • 1
  • 47
  • 66