2013-06-28 26 views
1

我想编辑数组字典中的特定值(在key:thumbnailImage下)。例如,我想将thepark.png改为theplace.png。我如何在目标C中做这件事?我也制作了一个plist的副本,以从appbundle的文档文件夹。在ios平台的数组字典中编辑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> 
+0

大概在info.plist中写入不是好主意,它不起作用,因为您正试图将字典写入应用程序包中的.plist文件,该文件夹是只读的。因此,它不会工作,也会有更多的拒绝机会。 – Smita

+0

我编辑了我的问题,实际上我已将一个副本发送到文档文件夹。 – user2531679

+1

http://stackoverflow.com/questions/1680367/changing-data-in-a-plist –

回答

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

非常感谢!这已经解决了我一直面临的问题! – user2531679

0

我不认为你可以编辑您的应用程序的资源文件的内容,所以你可能希望将其保存到文档文件夹编辑后...

+0

我已经复制plist到文档文件夹。然而我坚持编辑特定的数组索引值并将其保存 – user2531679

+0

//对于数组 BOOL result = [myArray writeToFile:filename atomically:YES]; // for a dictionary BOOL result = [myDict writeToFile:filename atomically:YES]; –