【发布时间】:2011-03-03 19:55:31
【问题描述】:
大家好, 我有这个列表:
<plist version="1.0">
<dict>
<key>Rows</key>
<array>
<dict>
<key>FavTitle</key>
<string>Book1</string>
<key>Favourited</key>
<string>duh</string>
<key>SaveName</key>
<string>book1.pdf</string>
</dict>
<dict>
<key>FavTitle</key>
<string>Book2</string>
<key>Favourited</key>
<string>duh</string>
<key>SaveName</key>
<string>book2.pdf</string>
</dict>
</array>
</dict>
</plist>
这样填充我的 UITableView:
cell.text = [dictionary objectForKey:@"FavTitle"];
(是的,这是贬值了)当一个单元格被选中时,我的字典对象被推送到我的详细视图(例如设置标题和 UIWebView url),如下所示:
NSDictionary *dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
DetailViewController *dvController = [[DetailViewController alloc] initWithNibName:@"DetailView" bundle:[NSBundle mainBundle]];
dvController.selectedSaveName = [dictionary objectForKey:@"SaveName"];
dvController.selectedFavTitle = [dictionary objectForKey:@"FavTitle"];
dvController.selectedFavourited = [dictionary objectForKey:@"Favourited"];
[dvController setHidesBottomBarWhenPushed:YES];
[self.navigationController pushViewController:dvController animated:YES];
[dvController release];
这一切都很好,我可以添加到该数组中,但是如何从中获取当前加载的字典?这不起作用:
NSString *documentsDirectory = [[NSString alloc] initWithString:[[[[NSBundle mainBundle] resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents/"]];
NSString *writablePath = [documentsDirectory stringByAppendingPathComponent:@"Favourites.plist"];
NSMutableDictionary *rootDict = [[NSMutableDictionary alloc] initWithContentsOfFile:writablePath];
NSMutableDictionary *thisFav = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"duh", @"Favourited",
selectedSaveName, @"SaveName",
selectedFavTitle, @"FavTitle",
nil];
[[rootDict objectForKey:@"Rows"] removeObject:thisFav];
[rootDict writeToFile:writablePath atomically: YES];
如何删除包含字符串“Book2”的条目?我发布的最后一个 sn-p 不起作用!谢谢大家!
【问题讨论】:
-
澄清:您想从 plist 中删除字典?例如。您必须输入:Book1 和 Book2,并且要删除 Book2。否则,问题根本就不清楚。
-
你说得对!现在编辑它,抱歉缺乏澄清,谢谢你指出那个人!
标签: iphone objective-c object plist nsdictionary