objective-c 我们如何存储到 NSDictionary 中?NSDictionary 和 NSMutableDictionary 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1760371/
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
How can we store into an NSDictionary? What is the difference between NSDictionary and NSMutableDictionary?
提问by Pradeep Reddy Kypa
I am developing an application in which I want to use an NSDictionary. Can anyone please send me a sample code explaining the procedure how to use an NSDictionaryto store Data with a perfect example?
我正在开发一个应用程序,我想在其中使用NSDictionary. 任何人都可以给我发送一个示例代码,解释如何使用NSDictionary一个完美的例子来存储数据的过程?
回答by Tim
The NSDictionaryand NSMutableDictionarydocs are probably your best bet. They even have some great examples on how to do various things, like...
在NSDictionary中和的NSMutableDictionary文档可能是你最好的选择。他们甚至有一些很好的例子来说明如何做各种事情,比如......
...create an NSDictionary
...创建一个 NSDictionary
NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:keys];
...iterate over it
...迭代它
for (id key in dictionary) {
NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
...make it mutable
...使其可变
NSMutableDictionary *mutableDict = [dictionary mutableCopy];
Note: historic version before 2010: [[dictionary mutableCopy] autorelease]
注:2010年之前的历史版本:[[dictionary mutableCopy] autorelease]
...and alter it
...并改变它
[mutableDict setObject:@"value3" forKey:@"key3"];
...then store it to a file
...然后将其存储到文件中
[mutableDict writeToFile:@"path/to/file" atomically:YES];
...and read it back again
...然后再读一遍
NSMutableDictionary *anotherDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"path/to/file"];
...read a value
...读取值
NSString *x = [anotherDict objectForKey:@"key1"];
...check if a key exists
...检查密钥是否存在
if ( [anotherDict objectForKey:@"key999"] == nil ) NSLog(@"that key is not there");
...use scary futuristic syntax
...使用可怕的未来派语法
From 2014 you can actually just type dict[@"key"] rather than [dict objectForKey:@"key"]
从 2014 年开始,您实际上可以只输入 dict[@"key"] 而不是 [dict objectForKey:@"key"]
回答by Ben Gottlieb
NSDictionary *dict = [NSDictionary dictionaryWithObject: @"String" forKey: @"Test"];
NSMutableDictionary *anotherDict = [NSMutableDictionary dictionary];
[anotherDict setObject: dict forKey: "sub-dictionary-key"];
[anotherDict setObject: @"Another String" forKey: @"another test"];
NSLog(@"Dictionary: %@, Mutable Dictionary: %@", dict, anotherDict);
// now we can save these to a file
NSString *savePath = [@"~/Documents/Saved.data" stringByExpandingTildeInPath];
[anotherDict writeToFile: savePath atomically: YES];
//and restore them
NSMutableDictionary *restored = [NSDictionary dictionaryWithContentsOfFile: savePath];
回答by jtbandes
The key difference: NSMutableDictionary can be modified in place, NSDictionary cannot. This is true for all the other NSMutable* classes in Cocoa. NSMutableDictionary is a subclassof NSDictionary, so everything you can do with NSDictionary you can do with both. However, NSMutableDictionary also adds complementary methods to modify things in place, such as the method setObject:forKey:.
关键区别:NSMutableDictionary 可以就地修改, NSDictionary 不能。这适用于 Cocoa 中的所有其他 NSMutable* 类。NSMutableDictionary 是NSDictionary的子类,所以你可以用 NSDictionary 做的一切你都可以做。但是,NSMutableDictionary 还添加了补充方法来修改内容,例如方法setObject:forKey:。
You can convert between the two like this:
您可以像这样在两者之间进行转换:
NSMutableDictionary *mutable = [[dict mutableCopy] autorelease];
NSDictionary *dict = [[mutable copy] autorelease];
Presumably you want to store data by writing it to a file. NSDictionary has a method to do this (which also works with NSMutableDictionary):
大概您想通过将数据写入文件来存储数据。NSDictionary 有一种方法可以做到这一点(也适用于 NSMutableDictionary):
BOOL success = [dict writeToFile:@"/file/path" atomically:YES];
To read a dictionary from a file, there's a corresponding method:
要从文件中读取字典,有一个相应的方法:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/file/path"];
If you want to read the file as an NSMutableDictionary, simply use:
如果要将文件作为 NSMutableDictionary 读取,只需使用:
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/file/path"];

