我想在用户退出视图时自动将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:]:零字符串参数'
任何想法我做错了什么?
你是什么意思的“它不工作”?当你运行你的代码时会发生什么? – jonkroll 2012-03-13 20:28:54
我没有出现在文档目录 – user1114881 2012-03-13 22:17:45
(您是否注意到您拼写错了文件名?您是否在错误文件的目录中查找?) – 2012-03-13 23:30:19