1

Hey guys, trying to add (object?) to a my plist programmatically, heres what I've cooked out so far:

 NSString *documentsDirectory = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents/myfolder"]];
        NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"];

        NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath];


        [rootDict setValue:@"My Third PDF" forKey:@"Title"];
        [rootDict writeToFile:writablePath atomically: YES];

and heres my plist:

<plist version="1.0">
<dict>
    <key>Rows</key>
    <array>
        <dict>
            <key>SaveName</key>
            <string>first.pdf</string>
            <key>Title</key>
            <string>My first PDF</string>
        </dict>
        <dict>
            <key>SaveName</key>
            <string>second.pdf</string>
            <key>Title</key>
            <string>My Second PDF</string>
        </dict>
    </array>
</dict>
</plist>

How do I go about adding another (object?) like

<dict>
            <key>SaveName</key>
            <string>third.pdf</string>
            <key>Title</key>
            <string>My third PDF</string>
        </dict>

to my plist? Thanks!

DexCurl
  • 1,683
  • 5
  • 26
  • 38

1 Answers1

2

You would fist have to make sure the Rows array is mutable:

[rootDict setObject:[[rootDict objectForKey:@"Rows"] mutableCopy] forKey:@"Rows"];

You would need to create a dict with that structure, e.g.

NSDictionary *third = [NDSictionary dictionaryWithObjectsAndKeys:
                       @"third.pdf", @"SaveName",
                       @"My third PDF", @"Title",
                       nil];

And add it to the array:

[[rootDict objectforKey:@"Rows"] addObject:third];
Anomie
  • 92,546
  • 13
  • 126
  • 145
  • Excellent answer thank you, I modified it slighty to be * NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath]; NSMutableDictionary *third = [NSMutableDictionary dictionaryWithObjectsAndKeys: @"third.pdf", @"SaveName", @"My third PDF", @"Title", nil]; [[rootDict objectForKey:@"Rows"] addObject:third];* – DexCurl Mar 01 '11 at 22:43