开发者

Create <dict> in array with keys in .plist (Cocoa/iPhone SDK)

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>
            <str开发者_运维知识库ing>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!


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];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜