如何在文件夹中保存文件? [英] How to save file in the documents folder?
本文介绍了如何在文件夹中保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将数据保存在iPhone 5.1模拟器上的文档文件夹中。
I am trying to save data in the documents folder on iPhone 5.1 simulator.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myData.json"];
if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
NSLog(@"Writable");
}else {
NSLog(@"Not Writable");
}
我总是不可写。
有什么想法吗?请帮助我。
I've got always "Not Writable". Any idea? Please help me.
推荐答案
可能因为您尚未创建文件,您测试的文件不存在。 :)
Maybe because you have not created the file, the file you tested does not exist. :)
你可以这样做来发现问题,
You can do this to find the problem,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory, @"myData.json"];
NSLog(@"filePath %@", filePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { // if file is not exist, create it.
NSString *helloStr = @"hello world";
NSError *error;
[helloStr writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}
if ([[NSFileManager defaultManager] isWritableFileAtPath:filePath]) {
NSLog(@"Writable");
}else {
NSLog(@"Not Writable");
}
这篇关于如何在文件夹中保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文