存储和检索多维NSMutableArrays的最佳方法是什么? [英] What's the best way to store and retrieve multi-dimensional NSMutableArrays?
问题描述
我将一堆数据存储在.plist文件中(在应用程序文档文件夹中),其结构如下:
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
)
)
}
如果我只是用
NSMutableDictionary * d = [[NSMutableDictionary alloc] initWithContentsOfFile:plistFile]
我将不会检索它能够替换数字对象,对吗?
所以我现在正在递归数据并形成整个事物的可变版本,它在一个实例中工作,但现在它告诉我发送到不可变对象的变异方法
当整个事情变得可变时。
If I just retrieve it with
NSMutableDictionary *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.
推荐答案
不应该写所有自定义类垃圾,你应该使用 NSPropertyListSerialization
。具体来说,请参阅 propertyListWithData:options:format:error:
方法。用法示例:
Instead of writing all that custom class junk, you should use NSPropertyListSerialization
. Specifically, see the propertyListWithData:options:format:error:
method. Example usage:
NSMutableDictionary *d = [NSPropertyListSerialization propertyListWithData:[NSData dataWithContentsOfFile:@"path/to/file"]
options:NSPropertyListMutableContainers
format:NULL
error:NULL];
这将使所有容器都可变,但保持叶节点(例如NSStrings)不可变。还有一个选项可以让树叶变得可变。
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.
这篇关于存储和检索多维NSMutableArrays的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!