如何从 plist 中更新和删除字符串项目和整个字典项目

发布于 2024-10-07 00:07:37 字数 356 浏览 2 评论 0原文

Root ---- Array
  Item 0- Dictionary
    fullName ---- String
    address ---- String
  Item 1 ---- Dictionary
    fullName ---- String
    address ---- String

我有以下 plist 结构。

Q1 在将代码中的 fullName 保存到 plist 文件中后,如何更新/更改代码中的 fullName?

Q2 如何仅删除一个字典项目(如上例中的第 0 项)?

我正在使用导航控制器 TableView 和详细示例。

谢谢

Root ---- Array
  Item 0- Dictionary
    fullName ---- String
    address ---- String
  Item 1 ---- Dictionary
    fullName ---- String
    address ---- String

I have the following plist structure.

Q1 How do I UPDATE/change fullName in code after it's already been saved in the plist file?

Q2 How do I DELETE just one Dictionary item say in the example above Item 0?

I am using the Navigation Controller TableView with the detail example.

Thanks

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

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

发布评论

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

评论(2

≈。彩虹 2024-10-14 00:07:37

您实际上并没有更新或删除 plist 文件中的某些内容。它比这更复杂一些:

  1. 将 plist 文件读入内存
  2. 更改内存版本
  3. 将新版本写回磁盘 读取

plist 非常简单。在您的情况下,由于根元素是一个数组,您可以这样做:

NSMutableArray * plistContents = [NSMutableArray arrayWithContentsOfFile:plistFilePath];

在这里我们创建一个可变数组,因为我们知道我们将更改其中的内容。如果我们只是想读取信息,我们应该使用 NSArray 而不是 NSMutableArray

更改内存版本就像操作标准容器一样简单:

//delete the first dictionary:
[plistContents removeObjectAtIndex:0];

//Change a dictionary's full name:
NSMutableDictionary * firstDictionary = [[plistContents objectsAtIndex:0] mutableCopy];
[firstDictionary setObject:@"Mike" forKey:@"fullName"];
[plistContents replaceObjectAtIndex:0 withObject:firstDictionary];
[firstDictionary release];

这里我们必须制作字典的可变(可更改)副本,因为在 plist 中读取只会创建不可变的 NSDictionary。因此,我们复制字典,更改我们想要更改的值,然后用我们新修改的字典替换原始字典。

将 plist 写入磁盘也很简单:

//be sure you have write-permissions first!
[plistContents writeToFile:plistFilePath atomically:YES];

You don't really update or delete something form the plist file. It's a bit more involved than that:

  1. Read the plist file into memory
  2. Alter the in-memory version
  3. Write the new version back out to disk

Reading in a plist is pretty trivial. In your case, since the root element is an array, you'd do:

NSMutableArray * plistContents = [NSMutableArray arrayWithContentsOfFile:plistFilePath];

Here we create a mutable array, since we know we're going to be changing stuff in it. If we just wanted to read the information, we should use NSArray instead of NSMutableArray.

Altering the in-memory version is as simple as manipulating standard containers:

//delete the first dictionary:
[plistContents removeObjectAtIndex:0];

//Change a dictionary's full name:
NSMutableDictionary * firstDictionary = [[plistContents objectsAtIndex:0] mutableCopy];
[firstDictionary setObject:@"Mike" forKey:@"fullName"];
[plistContents replaceObjectAtIndex:0 withObject:firstDictionary];
[firstDictionary release];

Here we have to make a mutable (changeable) copy of the dictionary, since reading in a plist only creates the immutable NSDictionary. So we copy the dictionary, change the value we want to change, and then replace the original dictionary with our new modified dictionary.

Writing the plist out to disk is also simple:

//be sure you have write-permissions first!
[plistContents writeToFile:plistFilePath atomically:YES];
一百个冬季 2024-10-14 00:07:37

您不直接操作 plist 中存储的数据,而是将对象序列化到 plist 或从 plist 反序列化对象。由于您想要更改数据,因此需要反序列化为可变对象。 NSPropertyListSerializationpropertyListWithData:options:format:error:当您使用 NSPropertyListMutableContainersNSPropertyListMutableContainersAndLeaves 选项。

NSError *err=nil;
NSData *raw = [[NSData alloc] initWithContentsOfFile:path];
id stuff = [NSPropertyListSerialization
                   propertyListWithData:raw
                                options:NSPropertyListMutableContainers 
                                 format:NULL
                                  error:&err;
      ];
if (err) {
    // couldn't parse the plist; handle the error.
    ...
    [err release];
}
[raw release];

这将为您提供 NSMutableArrayNSMutableDictionaryNSStringNSMutableString 的嵌套集合(具体取决于取决于您是否指定了 NSPropertyListMutableContainers 或 NSPropertyListMutableContainersAndLeaves)&c。反序列化 plist 后,使用集合提供的任何方法来删除(例如 NSMutableArray 的 removeObjectAtIndex:, <代码>NSMutableDictionary的removeObjectForKey:)或设置(例如 replaceObjectAtIndex:withObject:setObject:forKey:) 项。

有关更多信息,请阅读 属性列表编程指南

You don't operate on the data stored in a plist directly, you serialize objects to or unserialize objects from a plist. Since you want to alter the data, you need to unserialize to mutable objects. NSPropertyListSerialization's propertyListWithData:options:format:error: will unserialize a plist creating mutable collections when you use the NSPropertyListMutableContainers or NSPropertyListMutableContainersAndLeaves option.

NSError *err=nil;
NSData *raw = [[NSData alloc] initWithContentsOfFile:path];
id stuff = [NSPropertyListSerialization
                   propertyListWithData:raw
                                options:NSPropertyListMutableContainers 
                                 format:NULL
                                  error:&err;
      ];
if (err) {
    // couldn't parse the plist; handle the error.
    ...
    [err release];
}
[raw release];

This will get you a nested collection of NSMutableArrays, NSMutableDictionarys, either NSStrings or NSMutableStrings (depending on whether you specified NSPropertyListMutableContainers or NSPropertyListMutableContainersAndLeaves) &c. After you've unserialized the plist, use whatever method the collection provides to remove (e.g. NSMutableArray's removeObjectAtIndex:, NSMutableDictionary's removeObjectForKey:) or set (e.g. replaceObjectAtIndex:withObject:, setObject:forKey:) items.

For more info, read the Property List Programming Guide.

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