我想将数据保存在一个视图的属性列表中,并在另一个视图中检索它。我保存的数据结构如下所示:
键:(NSString)值(NSDictionary)
NSDictionary
仅包含键和值的字符串。每当按下按钮时,我都会触发此方法来进行保存:
- (IBAction)saveRSSItem:(id)sender {
NSMutableDictionary *dictionary;
if ([[NSFileManager defaultManager] fileExistsAtPath:[self dataFilePath]]){
dictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:[self dataFilePath]];
}
else {
dictionary = [[NSMutableDictionary alloc] init];
}
//Create Dictionary to store RSSItem information
NSMutableDictionary *RSSdictionary = [[NSMutableDictionary alloc] init];
if (self.selectedItem.title)
[RSSdictionary setValue:@"title" forKey:self.selectedItem.title];
if (self.selectedItem.summary)
[RSSdictionary setValue:@"summary" forKey:self.selectedItem.summary];
if (self.selectedItem.author)
[RSSdictionary setValue:@"author" forKey:self.selectedItem.author];
if (self.selectedItem.date)
[RSSdictionary setValue:@"date" forKey:self.selectedItem.date];
if (self.selectedItem.content)
[RSSdictionary setValue:@"content" forKey:self.selectedItem.content];
//Add saved RSSItem to dictionary
[dictionary setValue:RSSdictionary forKey:self.selectedItem.title];
//Overwrite the updated dictionary in the property list
NSURL *url = [[NSURL alloc] initFileURLWithPath:[self dataFilePath]];
[dictionary writeToURL:url atomically:YES];
}
当我尝试检索数据时,我会执行以下操作:
NSDictionary *savedRSSItemsDictionary = [NSDictionary dictionaryWithContentsOfFile:[self propertyListFilePath]];
NSDictionary *RSSDictionary = [savedRSSItemsDictionary objectForKey:[savedRSSItemsArray objectAtIndex:indexPath.row]];
第一行检索根字典。第二行检索属性列表中第一行的值(即 an NSDictionary
)。
但是,我不能使用 valueForKey: on 方法RSSDictionary
。它似乎不是一个NSDictionary
?任何想法为什么我无法NSDictionary
在属性列表的值字段中检索它?我需要使用NSPropertySerialization
吗?