2010-08-25 39 views
1

任何人都可以帮助正确加载字典对象到数组中。 将plist加载到字典对象中工作正常,我能够将其中的一部分输出到命令行中。 问题是,这种加载方式将每个名称和价格集合放入数组中的同一个单元格中。 IE: 是否可以单独加载它们?或者,也许在某种二维数组中有一个单元格用于名称和其他价格。解析词典从plist到数组

在此先感谢。

测试是一个可变数组。

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

而我的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> 

回答

1

你为什么不直接将其存储为一个数组?你的文件应该是这样的:

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

而且你可以做这样的事情:

NSArray *amountData = [NSArray arrayWithContentsOfFile: path]; 

否则这样做会是这样的正确方法:

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

问题的阵列的方法是,它返回每个行为: 行0:{ 名称= “属性1”; Price = 100000; } 我想知道如果更容易的方式加载这个,而不必手动解析括号等。 或者如果我可以直接从字典访问值拉出让我们说名称。 – totallhellfail 2010-08-25 15:56:33

+0

我不确定我完全理解你的问题。你会得到的是你想要的阵列。获取所有名称可以使用像这样的想法来完成:'[Test valueForKeyPath:@“name”]' - 返回一个包含所有名称的数组。 – 2010-08-25 18:26:24

+0

这听起来像我在找的东西; D谢谢。 – totallhellfail 2010-08-25 23:03:22

0

您正在致电

Test = [NSMutableArray arrayWithArray: amountData]; 

w hen amountData是一个NSDictionary,而不是一个数组......正如Max所建议的,你可以存储一个数组plist而不是一个字典plist。

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