2012-03-13 11 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]; 

それは私はそれがこの

<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> 

のように見えるテキストの出口でそれを開いたとき 1dictionaryという名前のファイルを持っています

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

+0

私はドキュメントディレクトリ – user1114881

+1

に表示されません(あなたはファイル名の誤りを覚えていますか?間違ったファイルについてdirを探していますか?) –

答えて

0

問題は、あなたがviewDidUnloadのテキストフィールドテキストを参照していることです。この時点で、ビューとそのサブビューはなくなりました。 -viewWillDisappearでこのコードを試してください。

+0

私は運がないviewWillDisappearで試しました。オーディオファイル、イメージファイル、テキストファイルを参照する文字列をこのディレクトリに置くことはできますか?ドキュメントディレクトリにはなくメインバンドルに存在するオブジェクトを参照できますか? – user1114881

+0

私はついに諦め、ボタンがタップされたときにそれを保存しました。今はうまく動作します。 Thx 4すべてのヘルプ – user1114881

0

保存と読み込みの方法で、ファイルパスを異なる方法でアセンブルします。私はロードファイルのパスから[dictionaryPath stringByAppendingFormat:@ "dictionary"]が足りないと思う。次に例外がスローされます[NSDictionary dictionaryWithContentsOfFile:fullPath];

関連する問題