在ios平台的数组字典中编辑Plist值

时间:2013-06-28 11:33:21

标签: ios plist

我想在数组字典中编辑特定值(在key:thumbnailImage下)。例如,我想将thepark.png更改为theplace.png。我如何在目标C中做到这一点?我还从appbundle中复制了plist to documents文件夹。

下面是我的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>

2 个答案:

答案 0 :(得分:3)

你需要读出plist作为可变字典编辑数据并回写。

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

答案 1 :(得分:0)

我认为您无法编辑应用资源中文件的内容,因此您可能希望在编辑后将其保存到Documents文件夹...