更新字典,保存在plist中
如果我尝试向我的字典(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 的结构如下所示:
我是针对 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:
I'm developing for iOS 5.0 on the iPad.
hope this helps
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论