0

ユーザーがビューを終了したときに、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>

私は今、このような 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:]: nil string parameter'

私が間違っていることを知っていますか?

4

2 に答える 2

0

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

于 2012-03-14T04:03:54.697 に答える
0

保存および読み込みメソッドでは、ファイル パスを別の方法で組み立てます。ロード ファイル パスから [dictionaryPath stringByAppendingFormat:@"dictionary"] が欠落していると思います。次に、例外がスローされます[NSDictionary dictionaryWithContentsOfFile:fullPath];

于 2014-03-01T20:31:18.133 に答える