简体   繁体   English

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

[英]How can we store into an NSDictionary? What is the difference between NSDictionary and NSMutableDictionary?

I am developing an application in which I want to use an NSDictionary . 我正在开发一个要使用NSDictionary的应用程序。 Can anyone please send me a sample code explaining the procedure how to use an NSDictionary to store Data with a perfect example? 任何人都可以给我发送示例代码来说明如何使用NSDictionary存储数据的过程的一个完美示例吗?

The NSDictionary and NSMutableDictionary docs are probably your best bet. NSDictionaryNSMutableDictionary文档可能是您最好的选择。 They even have some great examples on how to do various things, like... 他们甚至在如何做各种事情上都有很好的例子,例如...

...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]自动发行]

...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”]

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

The key difference: NSMutableDictionary can be modified in place, NSDictionary cannot . 关键区别: NSMutableDictionary可以就地修改,NSDictionary不能 This is true for all the other NSMutable* classes in Cocoa. 可可中的所有其他NSMutable *类都是如此。 NSMutableDictionary is a subclass of NSDictionary, so everything you can do with NSDictionary you can do with both. NSMutableDictionary是NSDictionary的子类 ,因此您可以对NSDictionary进行的所有操作都可以对两者进行处理。 However, NSMutableDictionary also adds complementary methods to modify things in place, such as the method setObject:forKey: . 但是,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"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM