如何将 NSMutableDictionary 保存到文档中的文件中?

发布于 2024-12-05 07:36:41 字数 250 浏览 0 评论 0原文

我想将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独﹏钓一江月 2024-12-12 07:36:41

NSMutableDictionaryNSDictionary 的子类:您可以在任何使用 NSDictionary 的地方使用它。从字面上看,只需将对象传递给您现在用于 NSDictionary 的相同代码即可。

从更一般的意义上来说,如果您确实需要从 NSMutableDictionary 获取真正不可变的 NSDictionary,只需在 上调用 copy 即可。 NSMutableDictionary

其他方法包括 [NSDictionary DictionaryWithDictionary:][NSDictionary alloc] initWithDictionary:],它们本质上都是同一件事。

NSMutableDictionary is a subclass of NSDictionary: you can use it anywhere you'd use NSDictionary. Literally, just pass the object through to the same code you use for NSDictionary right now.

In a more general sense, if you ever actually need to get a truly immutable NSDictionary from an NSMutableDictionary, just call copy on the NSMutableDictionary.

Other approaches include [NSDictionary dictionaryWithDictionary:] or [NSDictionary alloc] initWithDictionary:], which all amount to essentially the same thing.

乞讨 2024-12-12 07:36:41

如果您只是转到 NSDictionary 文档。您将看到有一种将字典保存到文件的方法

writeToFile:atomically:

将字典内容的属性列表表示形式写入给定路径。

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

参数

路径:
写入文件的路径。
如果路径包含波形符 (~) 字符,则在调用此方法之前必须使用 stringByExpandingTildeInPath 扩展它。

旗帜:
指定是否应以原子方式写入文件的标志。

如果flag为YES,则将字典写入辅助文件,然后将辅助文件重命名为path。如果flag为NO,则字典直接写入路径。 YES 选项保证该路径(如果存在)不会被损坏,即使系统在写入期间崩溃。

返回值

如果文件写入成功,则返回 YES,否则返回 NO。

该方法在写出文件之前递归地验证所有包含的对象是否都是属性列表对象(NSData、NSDate、NSNumber、NSString、NSArray 或 NSDictionary 的实例),如果所有对象都不是属性列表对象,则返回 NO,因为生成的文件不是有效的属性列表。

如果字典的内容都是属性列表对象,则通过该方法写入的文件可以通过类方法dictionaryWithContentsOfFile:或实例方法initWithContentsOfFile:来初始化一个新的字典。

因此,您正在寻找的代码可能类似于:

[myDict writeToFile:path atomically:YES]

其中 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.

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

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:

[myDict writeToFile:path atomically:YES]

where myDict is the dictionary you have and path is the path to the location you want to save it to

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文