1

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>
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Probably write in info.plist is not good idea and it doesn't work because you are trying to write the dictionary to a .plist file inside your App bundle, which is read only. Thus it would not work and also would have more rejection chances. – iSmita Jun 28 '13 at 11:37
  • I edited my question, i have actually made a copy out to the documents folder. – user2531679 Jun 28 '13 at 11:39
  • 1
    http://stackoverflow.com/questions/1680367/changing-data-in-a-plist – Vivek Sehrawat Jun 28 '13 at 11:47

2 Answers2

3

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];
Anupdas
  • 10,211
  • 2
  • 35
  • 60
0

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...

Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121
  • i have copied the plist to the documents folder. However i am stuck to edit the particular array index value and save it – user2531679 Jun 28 '13 at 11:40
  • // for an array BOOL result = [myArray writeToFile:filename atomically:YES]; // for a dictionary BOOL result = [myDict writeToFile:filename atomically:YES]; – Toseef Khilji Jun 28 '13 at 11:45