简体   繁体   English

将字典从plist解析为数组

[英]Parsing dictionary from plist into array

could anyone help with loading dictionary objects correctly into the array. 任何人都可以帮助将字典对象正确加载到数组中。 Loading of plist into dictionary object works fine, and I was able to output some of it into command-line. 将plist加载到字典对象中的工作正常,我能够将其中的一些输出到命令行中。 Problem is that this way of loading bundles each Name and Price sets into same cell in the array. 问题是这种将每个“名称”和“价格”集捆绑装入数组的同一单元格的方式。 IE: Is it possible to load them separately? IE:是否可以分别加载它们? Or perhaps in some sort of 2d array with one cell for name and other for price. 或者,在某种二维数组中,一个单元格用于名称,另一个单元格用于价格。

Thanks in advance. 提前致谢。

Test is a mutable array. 测试是可变数组。

NSString* path = [[NSBundle mainBundle] pathForResource:@"property" ofType:@"plist"];
NSDictionary* amountData =  [NSDictionary dictionaryWithContentsOfFile:path];
if (amountData) {
    Test = [NSMutableArray arrayWithArray: amountData];
}

And my plist structure is: 我的plist结构是:

<dict>
<key>New item</key>
<dict>
    <key>Name</key>
    <string>Property 1</string>
    <key>Price</key>
    <string>100000</string>
</dict>
<key>New item - 2</key>
<dict>
    <key>Name</key>
    <string>Property 2</string>
    <key>Price</key>
    <string>300000</string>
</dict>
<key>New item - 3</key>
<dict>
    <key>Name</key>
    <string>Property 3</string>
    <key>Price</key>
    <string>500000</string>
</dict>
<key>New item - 4</key>
<dict>
    <key>Name</key>
    <string>Property 4</string>
    <key>Price</key>
    <string>600000</string>
</dict>
</dict>

Why don't you directly store it as an array? 为什么不直接将其存储为数组? Your file would look like this: 您的文件如下所示:

<array>
    <dict>...</dict>
    <dict>...</dict>
    <dict>...</dict>
</array>

And you could do something like this: 您可以执行以下操作:

NSArray *amountData =  [NSArray arrayWithContentsOfFile: path];

Otherwise the right way to do it would be this: 否则,正确的方法是:

Test = [NSMutableArray arrayWithArray: [amountData allValues]];

You are making the call 您正在打电话

Test = [NSMutableArray arrayWithArray: amountData];

when amountData is an NSDictionary, not an array… As Max has suggested, you can just store an array plist instead of a dictionary plist. 当amountData是NSDictionary而不是数组时…正如Max所建议的,您可以只存储数组plist而不是字典plist。

NSString* path = [[NSBundle mainBundle] pathForResource:@"property" ofType:@"plist"];
NSDictionary* amountData =  [NSDictionary dictionaryWithContentsOfFile:path];
if (amountData) {
    Test = [NSMutableArray arrayWithArray: [amountData allValues]];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM