如何在 NSMutableDictionary 上使用 writeToFile 方法?

发布于 2024-12-05 07:44:18 字数 139 浏览 1 评论 0原文

我想使用仅适用于 NSDictionary 的方法 (writeToFile) 到 NSMutableDictionary 对象。那么,如何将这个 NSMutableDictionary 对象转换为 NSDictionary 呢?

I would like to use a method (writeToFile) only available for NSDictionary to a NSMutableDictionary object. So, how do I convert this NSMutableDictionary object to NSDictionary?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

并安 2024-12-12 07:44:18

NSMutableDictionary 继承自 NSDictionary。因此,writeToFile 应该对这两个类都可用。

NSMutableDictionary inherits from NSDictionary. So, writeToFile should be available to both classes.

怪异←思 2024-12-12 07:44:18

对于那些真正寻求从 NSMutableDictionary 到 NSDictionary 的转换的人:

NSMutableDictionary *mutableDict = [[[NSDictionary alloc] initWithObjectsAndKeys:
                              @"value", @"key"] mutableCopy];

NSDictionary *dict = [NSDictionary dictionaryWithDictionary:mutableDict]; //there you have it

For those coming here genuinely looking for conversion from NSMutableDictionary to NSDictionary :

NSMutableDictionary *mutableDict = [[[NSDictionary alloc] initWithObjectsAndKeys:
                              @"value", @"key"] mutableCopy];

NSDictionary *dict = [NSDictionary dictionaryWithDictionary:mutableDict]; //there you have it
九歌凝 2024-12-12 07:44:18

NSMutableDictionary 是 NSDictionary 的子类,因此 writeToFile 方法应该可用于您的对象,而无需进行任何转换或转换。

NSMutableDictionary is a subclass of NSDictionary, so the writeToFile method should be available for your object without having to do any casting or conversions.

厌倦 2024-12-12 07:44:18

正如此处已经讨论的:

如何保存将 NSMutableDictionary 转换为文档中的文件?

你不需要转换它。

但如果您确实想要,只需在 NSMutableDictionary 上使用 copy 方法或在 NSDictionary 上使用 dictionaryWithDictionary: 方法即可。两者都从 NSMutableDictionary 提供一个 NSDictionary

As already discussed here:

How to save a NSMutableDictionary into a file in documents?

you don't need to convert it.

But if you really want to, just use the copy method on your NSMutableDictionary or the dictionaryWithDictionary: method on NSDictionary. Both provide an NSDictionary from an NSMutableDictionary.

梦明 2024-12-12 07:44:18

NSMutableDictionary 是一个 NSDictionary,因为它是一个子类。通常,子类与其超类的关系称为:“是”。

A NSMutableDictionary is a NSDictionary since it is a subclass. Typically the relationship of a subclass to it's superclass is called: "is a".

停顿的约定 2024-12-12 07:44:18

要解决实际的问题标题而不是内容(因为我搜索了这个问题),您实际上可以依靠 copyWithZone: 来了解不可变字典。

NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];
mutableDict[@"key"] = @"value";
NSDictionary *immutableDict = [mutableDict copy];

To address the actual question title and not the contents (since I searched for this Q) you can actually rely on copyWithZone: to get down to an Immutable Dictionary.

NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc] init];
mutableDict[@"key"] = @"value";
NSDictionary *immutableDict = [mutableDict copy];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文