如何更新 plist 中的名称?

发布于 2024-11-28 08:06:13 字数 277 浏览 1 评论 0原文

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

这是 plist 文件

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

This is The plist File

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

定格我的天空 2024-12-05 08:06:13

也许这太明显了,但您可以使用以下方法来修改它:

  1. 进入 TextEdit 并修改内容
  2. 在 Xcode 中双击 plist 文件并使用 plist 编辑器进行编辑,该编辑器提供了更用户友好的 UI 来编辑 plist。
  3. 使用 NSKeyedArchiver 和 NSKeyedUnarchiver 类编写 ObjectiveC 代码来加载、修改和保存字典。

操作 plist 中存储的字典的终极解决方案:

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

Maybe It will be too obviously but you can use following ways to modify it:

  1. Go into TextEdit and modify content between
  2. In an Xcode double-click to plist file and edit it with plist editor that provides a more user friendly UI for editing plist.
  3. Write an ObjectiveC code with use of NSKeyedArchiver and NSKeyedUnarchiver classes to load, modify and save dictionary.

Ultimate solution for manipulating dictionaries stored in plists:

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

Hope you will adopt this code sample for your own needs.

... Ok, I got it here is a code sample for your particular plist structure:

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文