あるビューのプロパティ リストにデータを保存し、別のビューで取得したいと考えています。保存しているデータの構造は次のようになります。
キー: (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]];
最初の行は、ルート ディクショナリを取得します。2 行目は、プロパティ リストの最初の行の値 ( ) を取得しますNSDictionary
。
ただし、メソッド valueForKey: on は使用できませんRSSDictionary
。ではないようNSDictionary
です ? NSDictionary
プロパティリストの値フィールドにあるときに取得できない理由はありますか? を使用する必要がありますNSPropertySerialization
か?