如何从 plist 中更新和删除字符串项目和整个字典项目
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上并没有更新或删除 plist 文件中的某些内容。它比这更复杂一些:
plist 非常简单。在您的情况下,由于根元素是一个数组,您可以这样做:
在这里我们创建一个可变数组,因为我们知道我们将更改其中的内容。如果我们只是想读取信息,我们应该使用
NSArray
而不是NSMutableArray
。更改内存版本就像操作标准容器一样简单:
这里我们必须制作字典的可变(可更改)副本,因为在 plist 中读取只会创建不可变的 NSDictionary。因此,我们复制字典,更改我们想要更改的值,然后用我们新修改的字典替换原始字典。
将 plist 写入磁盘也很简单:
You don't really update or delete something form the plist file. It's a bit more involved than that:
Reading in a plist is pretty trivial. In your case, since the root element is an array, you'd do:
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 ofNSMutableArray
.Altering the in-memory version is as simple as manipulating standard containers:
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:
您不直接操作 plist 中存储的数据,而是将对象序列化到 plist 或从 plist 反序列化对象。由于您想要更改数据,因此需要反序列化为可变对象。
NSPropertyListSerialization
的propertyListWithData:options:format:error:
当您使用NSPropertyListMutableContainers
或NSPropertyListMutableContainersAndLeaves
选项。这将为您提供
NSMutableArray
、NSMutableDictionary
、NSString
或NSMutableString
的嵌套集合(具体取决于取决于您是否指定了 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
'spropertyListWithData:options:format:error:
will unserialize a plist creating mutable collections when you use theNSPropertyListMutableContainers
orNSPropertyListMutableContainersAndLeaves
option.This will get you a nested collection of
NSMutableArray
s,NSMutableDictionary
s, eitherNSString
s orNSMutableString
s (depending on whether you specifiedNSPropertyListMutableContainers
orNSPropertyListMutableContainersAndLeaves
) &c. After you've unserialized the plist, use whatever method the collection provides to remove (e.g.NSMutableArray
'sremoveObjectAtIndex:
,NSMutableDictionary
'sremoveObjectForKey:
) or set (e.g.replaceObjectAtIndex:withObject:
,setObject:forKey:
) items.For more info, read the Property List Programming Guide.