2012-03-13 123 views
0

我想在用戶退出視圖時自動將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:]:零字符串參數'

任何想法我做錯了什麼?

+0

你是什麼意思的「它不工作」?當你運行你的代碼時會發生什麼? – jonkroll 2012-03-13 20:28:54

+0

我沒有出現在文檔目錄 – user1114881 2012-03-13 22:17:45

+1

(您是否注意到您拼寫錯了文件名?您是否在錯誤文件的目錄中查找?) – 2012-03-13 23:30:19

回答

0

問題是,您正在引用viewDidUnload中的文本字段文本。此時,視圖及其子視圖消失。您應該在-viewWillDisappear中嘗試以下代碼:

+0

我在視圖中試過它會失敗,沒有運氣。我可以把字符串引用音頻文件,圖像文件和文本文件在這裏,我也可以引用不存在於文檔目錄中,但在主包中的對象? – user1114881 2012-03-14 18:15:51

+0

我終於放棄了,只是在按下按鈕時保存了它。現在工作正常。 Thx 4所有的幫助 – user1114881 2012-03-17 21:48:07

0

您在保存和加載方法中以不同方式組合文件路徑。我認爲你從加載文件路徑中遺漏了一個[dictionaryPathByAppendingFormat:@「dictionary」]。然後例外拋出[NSDictionary dictionaryWithContentsOfFile:fullPath];