Dave
Dave

Reputation: 4048

objective-c Add to/Edit .plist file

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?

Upvotes: 1

Views: 2237

Answers (2)

lionfly
lionfly

Reputation: 479

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.

Upvotes: 1

stefanB
stefanB

Reputation: 79810

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.

Upvotes: 1

Related Questions