如何将 NSMutableDictionary 保存到文档中的文件中?
我想将 NSMutableDictionary 对象的内容保存到文件中。我该怎么做?我已经知道如何使用 NSDictionary 对象完成此任务,但我不知道如何将此 NSMutableDictionary 转换/复制到 NSDictionary...除非有一种方法可以直接将 NSMutableDictionary 的内容写入文件...我强调NSMutableDictionary 对象包含 NSDictionary 类型的对象。
感谢您的帮助,
斯蒂芬
I would like to save the content of a NSMutableDictionary object to a file. How do I do this ? I already know how to do this task with a NSDictionary object but I don't know how to convert/copy this NSMutableDictionary to a NSDictionary...unless there's a method to write directly the content of NSMutableDictionary to a file...I stress that the NSMutableDictionary object contains objects of NSDictionary type.
Thx for helping,
Stephane
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSMutableDictionary
是NSDictionary
的子类:您可以在任何使用NSDictionary
的地方使用它。从字面上看,只需将对象传递给您现在用于NSDictionary
的相同代码即可。从更一般的意义上来说,如果您确实需要从
NSMutableDictionary
获取真正不可变的NSDictionary
,只需在上调用
。copy
即可。 NSMutableDictionary其他方法包括
[NSDictionary DictionaryWithDictionary:]
或[NSDictionary alloc] initWithDictionary:]
,它们本质上都是同一件事。NSMutableDictionary
is a subclass ofNSDictionary
: you can use it anywhere you'd useNSDictionary
. Literally, just pass the object through to the same code you use forNSDictionary
right now.In a more general sense, if you ever actually need to get a truly immutable
NSDictionary
from anNSMutableDictionary
, just callcopy
on theNSMutableDictionary
.Other approaches include
[NSDictionary dictionaryWithDictionary:]
or[NSDictionary alloc] initWithDictionary:]
, which all amount to essentially the same thing.如果您只是转到 NSDictionary 文档。您将看到有一种将字典保存到文件的方法
writeToFile:atomically:
将字典内容的属性列表表示形式写入给定路径。
参数
路径:
写入文件的路径。
如果路径包含波形符 (~) 字符,则在调用此方法之前必须使用 stringByExpandingTildeInPath 扩展它。
旗帜:
指定是否应以原子方式写入文件的标志。
如果flag为YES,则将字典写入辅助文件,然后将辅助文件重命名为path。如果flag为NO,则字典直接写入路径。 YES 选项保证该路径(如果存在)不会被损坏,即使系统在写入期间崩溃。
返回值
如果文件写入成功,则返回 YES,否则返回 NO。
该方法在写出文件之前递归地验证所有包含的对象是否都是属性列表对象(NSData、NSDate、NSNumber、NSString、NSArray 或 NSDictionary 的实例),如果所有对象都不是属性列表对象,则返回 NO,因为生成的文件不是有效的属性列表。
如果字典的内容都是属性列表对象,则通过该方法写入的文件可以通过类方法dictionaryWithContentsOfFile:或实例方法initWithContentsOfFile:来初始化一个新的字典。
因此,您正在寻找的代码可能类似于:
其中
myDict
是您拥有的字典,path
是您要将其保存到的位置的路径If you just swing over to the NSDictionary documents. You will see there is a method for saving a dictionary to a file
writeToFile:atomically:
Writes a property list representation of the contents of the dictionary to a given path.
Parameters
path:
The path at which to write the file.
If path contains a tilde (~) character, you must expand it with stringByExpandingTildeInPath before invoking this method.
flag:
A flag that specifies whether the file should be written atomically.
If flag is YES, the dictionary is written to an auxiliary file, and then the auxiliary file is renamed to path. If flag is NO, the dictionary is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.
Return Value
YES if the file is written successfully, otherwise NO.
This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.
If the dictionary’s contents are all property list objects, the file written by this method can be used to initialize a new dictionary with the class method dictionaryWithContentsOfFile: or the instance method initWithContentsOfFile:.
So the piece of code you are looking for is probably something like:
where
myDict
is the dictionary you have andpath
is the path to the location you want to save it to