我有一個主要字典,其中每個條目是一個字典。我需要將它保存到plist中,然後再檢索它的內容。IPHONE:從plist保存和檢索字典字典
這是我在做什麼,保存字典
// create a dictionary to store a fruit's characteristics
NSMutableDictionary *fruit = [[NSMutableDictionary alloc] init];
[fruit setObject:quantity forKey:@"quantity"];
[fruit setObject:productID forKey:@"productID"];
[fruit setObject:nameID forKey:@"nameID"];
// create a dictionary to store all fruits
NSMutableDictionary *stock = [[NSMutableDictionary alloc] init];
[stock setObject:fruit forKey:@"nameID"];
...添加所有水果的股票字典後,寫股票的plist中
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"stock.plist"];
NSMutableDictionary *stock = [NSMutableDictionary dictionaryWithContentsOfFile:path];
[stock writeToFile:path atomically:YES];
...到恢復字典,我用
NSMutableDictionary *stock = [NSMutableDictionary dictionaryWithContentsOfFile:path];
...但這不是保存任何東西的文件...我錯過了什麼?
感謝您的任何幫助。
謝謝!!!將這行[stock writeToFile:path atomically:YES]; 將字典的詞典序列化並保存到文件中? dictionaryWithContentsOfFile會讀取相同的結構嗎? – SpaceDog 2009-11-08 21:18:33