更新字典,保存在plist中

发布于 2024-12-12 02:17:18 字数 1009 浏览 0 评论 0原文

如果我尝试向我的字典(plist)添加新条目,它不起作用。编辑没有问题。我认为问题在于我如何尝试添加条目。

如果我删除以“setObject”开头的行,它将返回 YES。

我的代码如下所示:

 NSMutableDictionary *updateDict = [[self.plist objectForKey:@"comments"]mutableCopy];

 NSMutableDictionary *tmpDict = [[[NSMutableDictionary alloc]init]autorelease];  
 [tmpDict setObject:comment forKey:@"comment"];
 [tmpDict setObject:author forKey:@"author"];
 [tmpDict setObject:car forKey:@"car"];
 [tmpDict setObject:part forKey:@"part"];
 [tmpDict setObject:date forKey:@"date"];

 [updateDict setObject:tmpDict forKey:[NSNumber numberWithInt:[updateDict count]+1]];

 [self.plist setObject:updateDict forKey:@"comments"];
 if([self.plist writeToFile:self.plistPath atomically:YES]) {
return YES;
 }
 else {
     return NO;
 }

self.plist 是 plistPath 文件的本地副本。我的 plist 的结构如下所示:

https://img.skitch.com/20111026-tcjxp9ha4up8ggtfjy7ucgqcqe.png

我是针对 iPad 上的 iOS 5.0 进行开发。

希望这有帮助

It doesn't work, if I try to add a new entry to my dictionary (plist). Editing is no problem. I think the problem is, how I try to add the entry.

If I remove the lines starting with "setObject", it returns YES.

my code looks like:

 NSMutableDictionary *updateDict = [[self.plist objectForKey:@"comments"]mutableCopy];

 NSMutableDictionary *tmpDict = [[[NSMutableDictionary alloc]init]autorelease];  
 [tmpDict setObject:comment forKey:@"comment"];
 [tmpDict setObject:author forKey:@"author"];
 [tmpDict setObject:car forKey:@"car"];
 [tmpDict setObject:part forKey:@"part"];
 [tmpDict setObject:date forKey:@"date"];

 [updateDict setObject:tmpDict forKey:[NSNumber numberWithInt:[updateDict count]+1]];

 [self.plist setObject:updateDict forKey:@"comments"];
 if([self.plist writeToFile:self.plistPath atomically:YES]) {
return YES;
 }
 else {
     return NO;
 }

self.plist is my local copy of the file at plistPath. the structure of my plist looks like:

https://img.skitch.com/20111026-tcjxp9ha4up8ggtfjy7ucgqcqe.png

I'm developing for iOS 5.0 on the iPad.

hope this helps

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文