user2531679
user2531679

Reputation: 25

Edit Plist values in a dictionary of arrays for ios platform

I would like to edit particular value in a dictionary of arrays (under the key: thumbnailImage). For example i want to change thepark.png to theplace.png. How do i go about doing that in objective C? I also have made a copy of the plist to documents folder from the appbundle.

Below is the sample of my plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>location</key>
    <array>
       <string>The Park</string>
       <string>The Coffee Place</string>
       <string>The Center Fountain</string>
    </array>
    <key>timestamp</key>
    <array>
       <string>Not found yet</string>
       <string>Not found yet</string>
    </array>
    <key>thumbnailImage</key>
    <array>
       <string>thepark.png</string>
       <string>thecoffeeplace.png</string>
       <string>thecetnerfountain.png</string>
    </array>
</dict>
</plist>

Upvotes: 1

Views: 1483

Answers (2)

Anupdas
Anupdas

Reputation: 10201

You need to read out the plist as a mutable dictionary edit the data and write back.

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];

NSMutableArray *images = [dictionary[@"thumbnailImage"] mutableCopy];

//Apply your logic on how to change the value
NSString *newImage = @"theplace.png";
[images replaceObjectAtIndex:0 withObject:newImage];

dictionary[@"thumbnailImage"] = images;

[dictionary writeToFile:filePath atomically:YES];

Upvotes: 3

Toseef Khilji
Toseef Khilji

Reputation: 17409

I don't think you can edit the contents of a file in your app's resources, so you may want to save it to the Documents folder after editing...

Upvotes: 0

Related Questions