views:

49

answers:

1

I have a plist dictionary with several arrays where each have different number of items. These items are dictionaries too. So basically it looks like this:

  • Root dictionary
    • names array
    • places array
      • Item 0 dictionary
      • Item 1 dictionary
        • placeName string: "House"
        • description string: "My House"
        • height number: 10
      • Item 2 dictionary
      • ...
    • colors array
    • ...

I want to find the best way to change a value inside one of these arrays (placeName, description, height).

The plist is part of the resources, so I call it in like this:

[[NSMutableDictionary alloc] initFromName:@"mydefaults.plist"];

I've seen the setValue:forKey methods, but it looks like I'm heading into a mess. I don't think I should have to be setting the whole complete array if it's just one value.

So, which is the best way?

+1  A: 

How about something along the lines of

[[[plistDictionary objectForKey:@"places"] objectAtIndex:1] setValue:@"Another house" forKey:@"placeName"]

if the "internal" arrays and dictionaries are indeed mutable.

calmh
Cool! Very simple answer. I was extracting all the arrays to edit one.But... I did a NSLog(@"show: %@",plistDictionary); and it is updating, but when I run the app again, the values are not there.
elcool
are you writing the dictionary back out to the plist after editing it?
Ben Gottlieb
Yes; that you edit the dictionary in memory does not mean it gets saved to disk. You need a `[plistDictionary writeToFile:... atomically:...]` somwehere.
calmh
Thanks. I'm now reading the original plist the first time the app is launched and saving to a plist in the documentsDirectory for the next runs.
elcool