views:

51

answers:

3

Hi, 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 dictionaryWithContentsOfFile: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>
+1  A: 

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]];
Max Seelemann
Problem with array method is that it returns each row as:Row 0: { Name = "Property 1"; Price = 100000;}I was wondering if theres easier way of loading this, without having to manually parse brackets etc.Or if I could access value directly from Dictionary to pull out lets say Name.
totallhellfail
I'm not sure I totally understand your problem. What you'll get is the array you wanted. Getting all names could be done using somethink like this: `[Test valueForKeyPath: @"name"]` -- returns you an Array with all names.
Max Seelemann
This sounds like what I was looking for ;D Thanks.
totallhellfail
A: 

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.

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