存储和检索多维 NSMutableArray 的最佳方法是什么?
我将一堆数据存储在 .plist 文件中(在应用程序文档文件夹中),其结构如下:
Dictionary {
"description" = "String Value",
"sections" = Array (
Array (
Number,
...
Number
),
Array (
Number,
...
Number
)
),
"items" = Array (
Array (
Number,
...
Number
),
Array (
Number,
...
Number
)
)
}
如果我只是使用
检索它 NSMutableDictionary *d = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile]
我无法替换数字对象,对吗? 因此,我现在正在递归数据并形成整个事物的可变版本,并且它在一个实例中起作用,但现在它告诉我当整个事物发生时发送到不可变对象的变异方法
可变的。
有没有更简单/更好的方法来做到这一点?如果有什么不同的话,我的数据只是整数和布尔值。
I'm storing a bunch of data in a .plist file (in the application documents folder), and it's structured like this:
Dictionary {
"description" = "String Value",
"sections" = Array (
Array (
Number,
...
Number
),
Array (
Number,
...
Number
)
),
"items" = Array (
Array (
Number,
...
Number
),
Array (
Number,
...
Number
)
)
}
If I just retrieve it withNSMutableDictionary *d = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile]
I won't be able to replace the number objects, correct?
So I'm recursing through the data right now and forming a mutable version of the whole thing, and it worked in one instance, but now it's telling me mutating method sent to immutable object
when the whole thing is mutable.
Is there an easier/better way to do this? If it makes a difference, my data is just integers and booleans.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用 NSPropertyListSerialization,而不是编写所有自定义类垃圾。具体请参见
propertyListWithData:options:format:error:
方法。用法示例:这将使所有容器可变,但保持叶节点(例如 NSStrings)不可变。还有一个选项可以使叶子也可变。
Instead of writing all that custom class junk, you should use
NSPropertyListSerialization
. Specifically, see thepropertyListWithData:options:format:error:
method. Example usage:This will make all the containers mutable, but keep the leaf nodes (e.g. NSStrings) immutable. There's also an option to make the leaves mutable too.
我通常发现创建一个或多个自定义类来处理加载和保存更容易。这允许您显式地将数组转换为 mutableArrays:
完成此操作后,您可以将所有打包和解包代码封装在
loadFromFile
和saveToFile
方法中。这种方法的主要好处是您的主程序变得更加简单,并且它允许您将数据结构的元素作为属性访问:I usually find it easier to create one or more custom classes to handle loading and saving. This lets you convert the arrays to mutableArrays explicitly:
With that done, you can encapsulate all of the packaging and unpackaging code in your
loadFromFile
andsaveToFile
methods. The major benefit of this approach is that your main program gets a lot simpler, and it allows you to access the elements of your data structure as properties:你想要的是一个深度可变的副本。 Cocoa 不包含执行此操作的方法。之前有一些人编写过此类深度复制实现(示例)。
但是,Core Foundation 包含 CFPropertyList API,它既支持创建属性列表对象的深层可变副本,也支持从磁盘读取属性列表作为可变数据类型。 (当然,Core Foundation 的属性列表类型与 Cocoa 的属性列表类型是免费桥接的,这意味着您不必在它们之间进行转换 — NSArray 是 CFArray,反之亦然。)
What you want is a deep mutable copy. Cocoa doesn't include a way to do it. A few people have written such deep-copy implementations before (example).
However, Core Foundation includes the CFPropertyList API, which does have support both for creating deep mutable copies of property list objects as well as reading in property lists from disk as mutable datatypes. (And, of course, Core Foundation's property list types are toll-free bridged with Cocoa's, meaning you don't have to convert between them — an NSArray is a CFArray and vice-versa.)