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

您在保存和加载方法中以不同的方式组合文件路径。我认为您在加载文件路径中缺少 [dictionaryPath stringByAppendingFormat:@"dictionary"] 。然后抛出异常[NSDictionary dictionaryWithContentsOfFile:fullPath];

于 2014-03-01T20:31:18.133 回答
0

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

于 2012-03-14T04:03:54.697 回答