views:

408

answers:

2

Does writeToFile:atomically, add data to an existing .plist? Is it possible to modify a value in a .plist ? SHould the app be recompiled to take effect the change?

I have a custom .plist list in my app. The structure is as below:

<array>
<dict>
    <key>Title</key>
    <string>MyTitle</string>
    <key>Measurement</key>
    <dict>
        <key>prop1</key>
        <real>28.86392</real>
        <key>prop2</key>
        <real>75.12451</real>
    </dict>
    <key>Distance</key>
    <dict>
        <key>prop3</key>
        <real>37.49229</real>
        <key>prop4</key>
        <real>58.64502</real>
    </dict>
</dict>
</array>

The array tag holds multiple items with same structure. I need to add items to the existing .plist. Is that possible? I can write a UIView to do just that, if so.

*EDIT - OK, I just tried to writetofile hoping it would atleast overwrite if not add to it. Strangely, the following code selects different path while reading and writing.

NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

The .plist file is in my project. I can read from it. When I write to it, it is creating a .plist file and saving it in my /users/.../library/! Does this make sense to anyone?

A: 

I would assume that the plist that comes with application is assumed read only - it is inside the app bundle.

Any updates are stored in user specific directory, so that every user would have their own specific modifications.

stefanB
Absolutely correct. You should never write to any files in your application's bundle. Users without admin privileges generally don't have permission to do this. If you need to store persistent data, store it in `~/Library/Application Support` or use `NSUserDefaults`.
Rob Keniger
Thanks. Makes sense.
Dave
A: 

Of course you can create and write your own .plist file. By NSArray *arrayPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *userFile=[[arrayPath objectAtIndex:0] stringByAppendingString:@"/whatever.plist"]; , you can write/edit it.

But I also have the same question: How do I edit the value against a particular key, instead of overwriting the whole .plist file?

E.g. if I want to change the value "MyTitle" for the key "Title" in: <key>Title</key> <string>MyTitle</string> to "Sunflower", how can I do that from the code?

lionfly