我想在用戶退出視圖時自動將NSMutableDictionary保存到文檔目錄。這是我想要實現的代碼。它雖然沒有工作。有人可以幫我嗎。將NSMutableDictionary保存到文檔目錄
- (void)viewDidUnload
{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"audio.caf",@"pictureAudioKey",
@"audio.m4a",@"englishAudioKey",
@"audio2.m4a",@"spanishAudioKey",
@"audio3.m4a",@"frenchAudioKey",
@"audio4.m4a",@"germanAudioKey",
@"audio5.m4a",@"italianAudioKey",
@"audio6.m4a",@"chineseAudioKey",
@"image.jpg",@"photoimagekey",
@"name.txt", @"identity",
@"imagename.txt",@"numberkey",nil];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *dictionaryPath = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
dictionaryPath =[dictionaryPath stringByAppendingFormat:@"dicitonary" ] ;
NSDictionary *savedDictionary = dictionary;
NSLog(@"The Save file is:%@", savedDictionary);
[savedDictionary writeToFile:dictionaryPath atomically:YES];
[super viewDidUnload];
}
所以我得到了詞典通過把它與一個按鈕來保存到文檔目錄。 像這樣
- (void)saveAction:(id)sender {
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"audio.caf",@"pictureAudioKey",
@"image.jpg",@"photoimagekey",
@"name.txt", @"identity",
@"imagename.txt",@"numberkey",nil];
NSArray *documentPaths3 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory3 = [documentPaths3 objectAtIndex:0];
NSString *dictionaryPath3 = [documentsDirectory3 stringByAppendingPathComponent:[[self imageNameTextField]text]];
dictionaryPath3 =[dictionaryPath3 stringByAppendingFormat:@"dictionary" ] ;
NSDictionary *savedDictionary2 = dictionary;
NSLog(@"The Save file is:%@", savedDictionary2);
[savedDictionary2 writeToFile:dictionaryPath3 atomically:YES];
當我看到在我的文檔目錄中有一個名爲1dictionary 當我用文字退出它看起來像這樣
<key>identity</key>
<string>name.txt</string>
<key>numberkey</key>
<string>imagename.txt</string>
<key>photoimagekey</key>
<string>image.jpg</string>
<key>pictureAudioKey</key>
<string>audio.caf</string>
打開文件
I a現在正嘗試在像這樣的if語句中加載字典。
if ((int)index == 0) {
FinalDetailViewController *detailViewController = [[FinalDetailViewController alloc] initWithNibName:@"FinalDetailViewController" bundle:nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
//Get a full path to the image in the documents directory.
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:@"1dictionary"];
NSDictionary *myDictionary = [NSDictionary dictionaryWithContentsOfFile:fullPath];
NSMutableDictionary *familyDictionary =[[NSMutableDictionary alloc]initWithDictionary:myDictionary];
/*NSMutableDictionary *familyDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:
@"audio.caf",@"pictureAudioKey",
@"image.jpg",@"photoimagekey",
@"name.txt", @"identity",
@"imagename.txt",@"numberkey",nil];*/
detailViewController.familyDictionary = familyDictionary;
[self.navigationController pushViewController:detailViewController animated:YES];
}
,但它不走的應用程序崩潰與
*終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因是: '* - [NSURL initFileURLWithPath:]:零字符串參數'
任何想法我做錯了什麼?
你是什麼意思的「它不工作」?當你運行你的代碼時會發生什麼? – jonkroll 2012-03-13 20:28:54
我沒有出現在文檔目錄 – user1114881 2012-03-13 22:17:45
(您是否注意到您拼寫錯了文件名?您是否在錯誤文件的目錄中查找?) – 2012-03-13 23:30:19