将NSMutableDictionary保存到文档目录

时间:2012-03-13 20:02:50

标签: objective-c nsmutabledictionary nsdocumentdirectory

我想在用户退出视图时自动将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'

知道我做错了什么?

2 个答案:

答案 0 :(得分:0)

问题是您在viewDidUnload中引用了文本字段文本。此时,视图及其子视图已消失。您应该在-viewWillDisappear中尝试此代码:

答案 1 :(得分:0)

您在save和load方法中以不同方式组合文件路径。我认为你从加载文件路径中缺少[dictionaryPath stringByAppendingFormat:@“dictionary”]。然后在[NSDictionary dictionaryWithContentsOfFile:fullPath];

抛出异常