2009-11-19 63 views
66

我正在开发一个应用程序,我想在其中使用NSDictionary。任何人都可以给我一个示例代码,说明如何使用NSDictionary存储数据的过程如何完美的例子?我们如何储存到NSDictionary中? NSDictionary和NSMutableDictionary有什么区别?

+52

你可以通过具有看看NSDictionary的文档开始... – stefanB 2009-11-19 01:51:08

+7

礼貌RTFMs某种程度上似乎比明确的多那些腐蚀性。 – 2012-11-24 00:57:08

+1

@stefanB对于很多新程序员来说,官方文档通常非常复杂且令人困惑,因此SO上的_x_的基本视图可以提供帮助。 – 2017-07-21 18:56:11

回答

189

NSDictionaryNSMutableDictionary文档可能是您最好的选择。他们甚至对如何做各种事情,像一些伟大的例子...

......创建一个NSDictionary

NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil]; 
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil]; 
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects 
                 forKeys:keys];

......遍历它

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

...让它变为可变

NSMutableDictionary *mutableDict = [dictionary mutableCopy];

注:历史版本之前,2010:[辞典mutableCopy]自动释放]

......并改变它

[mutableDict setObject:@"value3" forKey:@"key3"];

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

[mutableDict writeToFile:@"path/to/file" atomically:YES];

...并将其读回aga在

NSMutableDictionary *anotherDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"path/to/file"];

......读值

NSString *x = [anotherDict objectForKey:@"key1"]; 

......检查是否存在关键

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

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

从2014年起,你其实可以只输入字典[@ “钥匙”]而不是[字典objectForKey:@ “钥匙”]

+0

对不起,最后一条评论有点不明显。感谢您指出。 – Tim 2009-11-19 01:46:08

+0

我没有看到它,所以你很好! :) – jtbandes 2009-11-19 07:16:56

+0

正确...有一个泄漏 – 2010-04-20 13:15:17

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]; 
+0

很好的解释。这是有帮助的.. – 2010-04-20 13:16:01

+0

@本戈特利布 第一部分: 感谢您的好解释。实际上我想将我的两个UItextField数据保存在MutableDictionary的文件中。在这里你写了'NSString * savePath = [@“〜/ Documents/Saved.data”stringByExpandingTildeInPath];',现在我的问题是,这里的Saved.data文件是什么?我怎么做到的? – Tulon 2014-03-25 11:48:58

+0

@Ben Gottlieb第二部分: 在我的情况下,我把一个文本文件(.rtf)放在以下目录下:'Support files> MediaFiles> Documents> userData.rtf'.Now can save this type of Mutabledictionary data in text file ?在这种情况下,完美的目录是什么?我正在用这个'[userData writeToFile:@“MediaFiles/Documents/userData.rtf”atomically:YES];'用这个'[userData writeToFile:@“Documents/userData.rtf”atomically:YES];',但是没有工作。 祝您有美好的一天。 – Tulon 2014-03-25 11:49:16

18

关键的区别:的NSMutableDictionary可以就地修改的NSDictionary不能。对于Cocoa中的所有其他NSMutable *类都是如此。 NSMutableDictionary是NSDictionary的子类,所以你可以用NSDictionary做的所有事情都可以用两者来完成。但是,NSMutableDictionary还添加了补充方法来修改适当位置的内容,例如方法setObject:forKey:

您可以在两个这样的之间的转换:

NSMutableDictionary *mutable = [[dict mutableCopy] autorelease]; 
NSDictionary *dict = [[mutable copy] autorelease]; 

大概希望将其写入文件来存储数据。NSDictionary中有一个方法来做到这一点(也可与的NSMutableDictionary):

BOOL success = [dict writeToFile:@"/file/path" atomically:YES]; 

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

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/file/path"]; 

如果你想阅读的文件一个NSMutableDictionary,只需使用:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:@"/file/path"]; 
+0

感谢您的回复... – 2010-04-20 13:14:16

相关问题