如何在 NSMutableDictionary 上使用 writeToFile 方法?
我想使用仅适用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
NSMutableDictionary 继承自 NSDictionary。因此,writeToFile 应该对这两个类都可用。
NSMutableDictionary inherits from NSDictionary. So, writeToFile should be available to both classes.
对于那些真正寻求从 NSMutableDictionary 到 NSDictionary 的转换的人:
For those coming here genuinely looking for conversion from NSMutableDictionary to NSDictionary :
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.
正如此处已经讨论的:
如何保存将 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 yourNSMutableDictionary
or thedictionaryWithDictionary:
method onNSDictionary
. Both provide anNSDictionary
from anNSMutableDictionary
.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".
要解决实际的问题标题而不是内容(因为我搜索了这个问题),您实际上可以依靠
copyWithZone:
来了解不可变字典。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.