如何在plist中更新名称?

时间:2011-08-09 02:51:05

标签: iphone plist

<plist version="1.0">
<dict>
<key>accounts</key>
<array>
    <dict>
        <key>name</key>
        <string>OLDNAME</string>
    </dict>
</array>
</dict>

这是plist文件

2 个答案:

答案 0 :(得分:2)

也许它太明显但你可以用以下方法来修改它:

  1. 进入TextEdit并修改
  2. 之间的内容
  3. 在Xcode中双击plist文件并使用plist编辑器进行编辑,该编辑器为编辑plist提供了更加用户友好的UI。
  4. 使用NSKeyedArchiver和NSKeyedUnarchiver类编写ObjectiveC代码,以加载,修改和保存字典。
  5. 操纵存储在plists中的词典的终极解决方案:

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"test" ofType:@ "plist"];
    NSDictionary *plistDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    [plistDictionary setValue:@"newName" forKey:@"testKey"];
    [plistDictionary writeToFile:plistPath atomically:YES];
    

    希望您能根据自己的需要采用此代码示例。

    ...好的,我在这里得到的是您特定的plist结构的代码示例:

    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"yourPlist" ofType:@ "plist"];
    NSMutableDictionary *plistDictionary = [[NSDictionary dictionaryWithContentsOfFile:plistPath] mutableCopy];
    NSMutableArray *accountsArray = [(NSArray *)[plistDictionary valueForKey:@"accounts"] mutableCopy];
    NSMutableDictionary *accountDictionary = [(NSDictionary *)[accountsArray objectAtIndex:0] mutableCopy];
    
    [accountDictionary setValue:@"NewName" forKey:@"name"];
    [accountsArray replaceObjectAtIndex:0 withObject:accountDictionary];
    [plistDictionary setValue:accountsArray forKey:@"accounts"];
    
    [plistDictionary writeToFile:plistPath atomically:YES];
    

答案 1 :(得分:0)

Apple有很多关于这个主题的文档。

这是应该有所帮助的:

http://developer.apple.com/library/mac/#documentation/CoreFoundation/Conceptual/CFPropertyLists/CFPropertyLists.html

相关问题