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



我想编辑数组字典中的特定值(在键thumbnailImage下)。例如,我想把park.png改为place.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>

您需要将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];

我认为你无法在应用程序的资源中编辑文件的内容,所以你可能想在编辑后将其保存到Documents文件夹中。。。

最新更新