开发者

Parsing dictionary from plist into array

开发者 https://www.devze.com 2023-01-12 21:01 出处:网络
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.

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. 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? 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 dictionaryWithConte开发者_如何学CntsOfFile:path];
if (amountData) {
    Test = [NSMutableArray arrayWithArray: amountData];
}

And my plist structure is:

<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.


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

精彩评论

暂无评论...
验证码 换一张
取 消