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