将 NSMutableDictionary 保存到文档目录
Posted
技术标签:
【中文标题】将 NSMutableDictionary 保存到文档目录【英文标题】:saving a NSMutableDictionary to the documents directory 【发布时间】:2012-03-13 20:02:50 【问题描述】:我想在用户退出视图时自动。这是我要实现的代码。但它不起作用。谁能帮帮我。
- (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”
知道我做错了什么吗?
【问题讨论】:
“它不起作用”是什么意思?运行代码时会发生什么? 我没有出现在文档目录中 (你有没有发现文件名拼错了?你在目录里找错文件了吗?) 感谢您看到这一点,但这不是问题所在。我正在计算机上查看模拟器的文档文件。我可以看到保存在那里的其他东西,但看不到这个文件。我希望将字典保存为(无论用户在文本字段中输入什么),然后是单词字典。它不存在。这可能吗? 【参考方案1】:问题是您在 viewDidUnload 中引用了文本字段文本。此时,视图及其子视图都消失了。您应该在 -viewWillDisappear 中尝试此代码:
【讨论】:
我在 viewWillDisappear 中尝试过,但没有成功。我可以在其中放置引用音频文件、图像文件和文本文件的字符串,还可以引用文档目录中不存在但在主包中的对象吗? 我终于放弃了,只是在点击按钮时保存了它。现在工作正常。谢谢 4 所有的帮助【参考方案2】:您在保存和加载方法中以不同的方式组合文件路径。我认为您在加载文件路径中缺少 [dictionaryPath stringByAppendingFormat:@"dictionary"] 。然后在[NSDictionary dictionaryWithContentsOfFile:fullPath];
抛出异常@
【讨论】:
以上是关于将 NSMutableDictionary 保存到文档目录的主要内容,如果未能解决你的问题,请参考以下文章
将 NSMutableDictionary 保存到 plist iOS
将 NSMutableDictionary 保存到 plist 问题中
读取和写入 NSMutableDictionary 到 plist 文件