我们如何存储到NSDictionary中?NSDictionary和NSMutableDictionary有什么区别?

Pra*_*ypa 68 iphone objective-c nsdictionary nsmutabledictionary

我正在开发一个我想要使用的应用程序NSDictionary.任何人都可以给我一个示例代码,解释如何使用NSDictionary一个完美的例子来存储数据的过程?

Tim*_*Tim 191

NSDictionary中的NSMutableDictionary文档可能是你最好的选择.他们甚至有一些关于如何做各种事情的好例子,比如......

...创建一个NSDictionary

NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects 
                                                       forKeys:keys];
Run Code Online (Sandbox Code Playgroud)

...迭代它

for (id key in dictionary) {
    NSLog(@"key: %@, value: %@", key, [dictionary objectForKey:key]);
}
Run Code Online (Sandbox Code Playgroud)

......让它变得可变

NSMutableDictionary *mutableDict = [dictionary mutableCopy];
Run Code Online (Sandbox Code Playgroud)

注:2010年之前的历史版本:[[dictionary mutableCopy] autorelease]

......并改变它

[mutableDict setObject:@"value3" forKey:@"key3"];
Run Code Online (Sandbox Code Playgroud)

...然后将其存储到文件中

[mutableDict writeToFile:@"path/to/file" atomically:YES];
Run Code Online (Sandbox Code Playgroud)

......然后再读一遍

NSMutableDictionary *anotherDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"path/to/file"];
Run Code Online (Sandbox Code Playgroud)

...读一个值

NSString *x = [anotherDict objectForKey:@"key1"];
Run Code Online (Sandbox Code Playgroud)

...检查是否存在密钥

if ( [anotherDict objectForKey:@"key999"] == nil ) NSLog(@"that key is not there");
Run Code Online (Sandbox Code Playgroud)

...使用可怕的未来语法

从2014年开始,您实际上只需键入dict [@"key"]而不是[dict objectForKey:@"key"]

  • 蒂姆,因为你很棒的答案是五岁(时间过得真快),我只是为ARC编辑了它.如果你不挖掘,显然只是放松!干杯! 想想你在这里帮助了多少初学者. (2认同)

Ben*_*ieb 32

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];
Run Code Online (Sandbox Code Playgroud)


jtb*_*des 18

关键区别:NSMutableDictionary可以修改到位,NSDictionary不能.对于Cocoa中的所有其他NSMutable*类都是如此.NSMutableDictionary是NSDictionary 的子类,因此您可以使用NSDictionary执行所有操作.但是,NSMutableDictionary还添加了补充方法来修改现有的东西,例如方法setObject:forKey:.

你可以像这样在两者之间进行转换:

NSMutableDictionary *mutable = [[dict mutableCopy] autorelease];
NSDictionary *dict = [[mutable copy] autorelease]; 
Run Code Online (Sandbox Code Playgroud)

据推测,您希望通过将数据写入文件来存储数据.NSDictionary有一个方法可以做到这一点(也适用于NSMutableDictionary):

BOOL success = [dict writeToFile:@"/file/path" atomically:YES];
Run Code Online (Sandbox Code Playgroud)

要从文件中读取字典,有一个相应的方法:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/file/path"];
Run Code Online (Sandbox Code Playgroud)

如果要将文件作为NSMutableDictionary读取,只需使用:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/file/path"];
Run Code Online (Sandbox Code Playgroud)