objective-c 如何更新 NSMutableDictionary?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2227400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:54:57  来源:igfitidea点击:

How to update NSMutableDictionary?

iphoneobjective-cnsdictionarynsmutabledictionary

提问by S.P.

I have an NSMutableDictionary.

我有一个 NSMutableDictionary。

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

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

我必须更新该字典中的一个元素。我怎么能这样做?

回答by Mihir Mathuria

Please refer to the APIhere

请参考此处的API

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

回答by pedrotorres

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

字典只允许您为每个键拥有一个对象,因此如果您使用“为键设置对象”,它将更新当前的对象

回答by Renetik

NSMutableDictionary's method addEntriesFromDictionary.

NSMutableDictionary 的方法addEntriesFromDictionary

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

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.

如果两个字典都包含相同的键,则接收字典的该键的先前值对象将被发送一个释放消息,并且新值对象取代它。