Replace a value in NSDictionary in iPhone

僤鯓⒐⒋嵵緔 提交于 2019-11-28 04:42:25
donkim

Here's what you can do:

NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
NSDictionary *oldDict = (NSDictionary *)[dataArray objectAtIndex:0];
[newDict addEntriesFromDictionary:oldDict];
[newDict setObject:@"Don" forKey:@"Name"];
[dataArray replaceObjectAtIndex:0 withObject:newDict];
[newDict release];

Hope this helps!

vodkhang

You first need an NSMutableDictionary with it you can change the key and value.

It would be like this:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"david", @"name", "85", @"marks", nil];

[dict setObject:@"90" forKey:@"david"];

If you want to update an object in a NSDictionary you should use NSMutableDictionary instead. NSMutableDictionary has the following EXTRA methods

Adding Entries

  • setObject:forKey:
  • setValue:forKey:
  • addEntriesFromDictionary:
  • setDictionary:

Removing Entries

  • removeObjectForKey:
  • removeAllObjects
  • removeObjectsForKeys:
 // NSDictionary *dict = ...
    NSMutableDictionary *mutableDict = [dict mutableCopy];
    [mutableDict setObject:@"myObject" forKey:@"myKey"];
    dict = [mutableDict mutableCopy];

I hope it helps

[davidsRecord setObject:@"100" forkey:@"mark"];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!