将字典添加到 plist

发布于 2024-10-12 23:13:39 字数 966 浏览 2 评论 0原文

我希望有人可以帮助我,当我选择一个已经可以工作但只写入一个条目的 tableView 行时,我想将一本字典写入 plist。但每个选择都应该添加。

到目前为止我的代码:

NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"liste.plist"];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [selectedEntry valueForKey:NAME], @"Name",
                                  [selectedEntry valueForKey:add], @"Add"
                                  , nil];

[tempArray writeToFile:plistPath atomically:YES];

我找到了解决问题的方法。 我在 viewDidLoad 中初始化数组并用 plist 填充它,现在它可以工作了......

self.tempArray = [NSMutableArray arrayWithContentsOfFile:plistPath]; 

并更改

[dictionary writeToFile:plistPath atomically:YES];

[tempArray writeToFile:plistPath atomically:YES];

I hope someone can help me with that I want to write a dictionary to a plist when i select a tableView row which already works but it only writes one entry. but every selection should be added.

my code so far:

NSString *plistPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"liste.plist"];

NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys: 
                                  [selectedEntry valueForKey:NAME], @"Name",
                                  [selectedEntry valueForKey:add], @"Add"
                                  , nil];

[tempArray writeToFile:plistPath atomically:YES];

I found a way to solve the problem.
I initialize the Array in the viewDidLoad and filled it with the plist and now it works....

self.tempArray = [NSMutableArray arrayWithContentsOfFile:plistPath]; 

and changed

[dictionary writeToFile:plistPath atomically:YES];

to

[tempArray writeToFile:plistPath atomically:YES];

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

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

发布评论

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

评论(3

残月升风 2024-10-19 23:13:39

我认为你应该将所有字典放入一个可变数组中,该数组将成为 plist 的根对象。否则,您应该想出一个解决方案来为添加的每个条目生成新密钥。

当您需要添加内容时,只需从 plist 中读取可变数组,添加到其中并保存即可。据我所知,数组和字典在读/写 plist 时的方法非常相似。

I think that you should put all dictionaries into a mutable array which would be the root object of the plist. Otherwise you should come up with a solution to generate new keys for each entry added.

When you need to add stuff you just read up the mutable array from the plist, add to it, and save it. From what I recall, the methods are very similar between arrays and dictionaries when reading/writing plists.

九歌凝 2024-10-19 23:13:39

这是我使用的:

NSFileManager *manager = [NSFileManager defaultManager];
NSMutableDictionary *dict;  
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
        VALUE1, KEY1,
        VALUE2, KEY2,
        VALUE3, KEY3,
        VALUE4, KEY4,
        nil ];
NSString *err = nil;
NSData *plist;
plist = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 errorDescription:&err];

if([manager fileExistsAtPath:@"YourPlist.plist"] == NO){
    [manager createFileAtPath:[NSString stringWithFormat:@"YourPlist.plist"] contents:plist attributes:nil];
}
[manager release];

Here is what I use:

NSFileManager *manager = [NSFileManager defaultManager];
NSMutableDictionary *dict;  
dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
        VALUE1, KEY1,
        VALUE2, KEY2,
        VALUE3, KEY3,
        VALUE4, KEY4,
        nil ];
NSString *err = nil;
NSData *plist;
plist = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListBinaryFormat_v1_0 errorDescription:&err];

if([manager fileExistsAtPath:@"YourPlist.plist"] == NO){
    [manager createFileAtPath:[NSString stringWithFormat:@"YourPlist.plist"] contents:plist attributes:nil];
}
[manager release];
你列表最软的妹 2024-10-19 23:13:39

如果 selectedEntry 中的任一对象不是 NSString、NSNumber、NSArray、NSDictionary 等,则写入将不会成功:

来自 NSDictionary 参考文档:

此方法递归地验证所有包含的对象是否为属性列表对象(实例NSData、NSDate、NSNumber、NSString、NSArray 或 NSDictionary)在写出文件之前,如果所有对象都不是属性列表对象,则返回 NO,因为生成的文件不是有效的属性列表。

If either object from selectedEntry is not a NSString, NSNumber, NSArray, NSDictionary, etc. then the write will not succeed:

From the NSDictionary reference document:

This method recursively validates that all the contained objects are property list objects (instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary) before writing out the file, and returns NO if all the objects are not property list objects, since the resultant file would not be a valid property list.

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