1

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

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

2 回答 2

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];
于 2013-06-28T11:46:23.657 回答
0

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

于 2013-06-28T11:39:12.127 回答