Iphone 将字典从plist解析为数组

Iphone 将字典从plist解析为数组,iphone,objective-c,plist,Iphone,Objective C,Plist,任何人都可以帮助将字典对象正确加载到数组中。 将plist加载到dictionary对象中效果很好,我能够将其中的一部分输出到命令行。 问题是,这种方法将每个名称和价格集加载到数组中的同一单元格中。 即: 可以单独加载它们吗?或者在某种2d数组中,一个单元格表示名称,另一个单元格表示价格 提前谢谢 Test是一个可变数组 NSString* path = [[NSBundle mainBundle] pathForResource:@"property" ofType:@"plist"]; NS

任何人都可以帮助将字典对象正确加载到数组中。 将plist加载到dictionary对象中效果很好,我能够将其中的一部分输出到命令行。 问题是,这种方法将每个名称和价格集加载到数组中的同一单元格中。 即: 可以单独加载它们吗?或者在某种2d数组中,一个单元格表示名称,另一个单元格表示价格

提前谢谢

Test是一个可变数组

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
价格
100000
新项目-2
名称
财产2
价格
300000
新项目-3
名称
财产3
价格
500000
新项目-4
名称
财产4
价格
600000

为什么不直接将其存储为数组?您的文件如下所示:

<array>
    <dict>...</dict>
    <dict>...</dict>
    <dict>...</dict>
</array>
NSArray *amountData =  [NSArray arrayWithContentsOfFile: path];
否则,正确的做法是:

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

为什么不直接将其存储为数组?您的文件如下所示:

<array>
    <dict>...</dict>
    <dict>...</dict>
    <dict>...</dict>
</array>
NSArray *amountData =  [NSArray arrayWithContentsOfFile: path];
否则,正确的做法是:

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

你在打电话

Test = [NSMutableArray arrayWithArray: amountData];

当amountData是NSDictionary而不是数组时……正如Max所建议的,您可以只存储数组plist而不是字典plist。

您正在进行调用

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

如果amountData是NSDictionary,而不是数组…正如Max所建议的,您可以只存储数组plist而不是字典plist。

array方法的问题是它将每一行返回为:row 0:{Name=“Property 1”;Price=100000;}我想知道是否有更简单的加载方法,不需要手动解析括号等,或者如果我可以直接从字典中访问值来提取,让我们说一下名称。我不确定我是否完全理解您的问题。您将得到您想要的阵列。可以使用类似这样的方法获取所有名称:
[Test valueForKeyPath:@“name”]
--返回一个包含所有名称的数组。这听起来像是我在寻找的;D谢谢。数组方法的问题是它返回每一行的方式是:行0:{Name=“Property 1”;Price=100000;}我想知道是否有更简单的方法来加载它,不需要手动解析括号等,或者如果我可以直接从字典中访问值来提取,让我们说一下名称。我不确定我是否完全理解您的问题。您将得到您想要的阵列。可以使用类似这样的方法获取所有名称:
[Test valueForKeyPath:@“name”]
--返回一个包含所有名称的数组。这听起来像是我在寻找的;D谢谢。
NSString* path = [[NSBundle mainBundle] pathForResource:@"property" ofType:@"plist"];
NSDictionary* amountData =  [NSDictionary dictionaryWithContentsOfFile:path];
if (amountData) {
    Test = [NSMutableArray arrayWithArray: [amountData allValues]];
}