Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

Writing

LinusRLinusR Posts: 17Registered Users
Hi all,

I have a problem: I have a property list looking like this:

<dict>
<key>fruits</key>
<dict>
<key>fruit1</key>
<string>Banana</string>
<key>fruit2</key>
<string>Apple</string>
</dict>
</dict>

I would like to change the value of fruit 1. How can I do this? My actual code is:

NSString *fileDirectory;
fileDirectory = [[[NSBundle mainBundle] pathForResource:@\"Fruits\" ofType:@\"plist\"] retain];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fileDirectory];

I don't know what to do now. My actual not working code is:

[plistDict setValue:fruit1 forKey:@\"Peach\"];
[plistDict writeToFile:fileDirectory atomically: YES];

Now a new key and value gets build.

Can anyone help?

Thanks for every help
Post edited by LinusR on

Replies

  • MiniRobinhoMiniRobinho Posts: 160Registered Users
    You've accessed the plist fine, but you are setting the value wrong.

    First get the dictionary with key "fruits":
    NSMutableDictionary *fruits = [plistDict objectForKey:@\"fruits\"];


    In fruits set the value "Peach" for the key "fruit1":
    [fruits setValue:@\"Peach\" forKey:@\"fruit1\"];
    [plistDict writeToFile:fileDirectory atomically: YES];


    Hope that makes sense. If you don't know why certain things have been done just say
  • LinusRLinusR Posts: 17Registered Users
    Thanks for help, but I think I do something wrong.
    Now I have code like this:
    NSString *fileDirectory;
    fileDirectory = [[[NSBundle mainBundle] pathForResource:@\"Datas\" ofType:@\"plist\"] retain];
    NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:fileDirectory];

    NSMutableDictionary *plistDictName = [plistDict objectForKey:@\"Fruit\"];

    [plistDictName setValue:@\"Apple\" forKey:@\"fruit1\"];


    But it doesn't work. Then I created a new dict in the Plist file "Datas.plist" with the name "Fruit" but that does not work as well. My "Datas.plist" file looks like this now:

    <dict>
    <key>Fruit</key>
    <dict>
    <key>fruit1</key>
    <string></string>
    </dict>
    </dict>


    Nothing happens when I click the button in my application. The string of the key "fruit1" keeps empty.

    What do I wrong?
  • MiniRobinhoMiniRobinho Posts: 160Registered Users
    Try the below - setObject instead of setValue.

    [plistDictName setObject:@\"Apple\" forKey:@\"fruit1\"];


    Make sure that you are saving at the end as well:

    [plistDict writeToFile:fileDirectory atomically: YES];
  • LinusRLinusR Posts: 17Registered Users
Sign In or Register to comment.