Ios 读取文档文件夹中的文本文件-Iphone SDK
我有以下代码:Ios 读取文档文件夹中的文本文件-Iphone SDK,ios,iphone,objective-c,cocoa,documents,Ios,Iphone,Objective C,Cocoa,Documents,我有以下代码: NSString *fileName = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentDownload"]; NSString *fullPath = [NSBundle pathForResource:fileName ofType:@"txt" inDirectory:[NSHomeDirectory() stringByAppendingString:@"/Documents/"]]
NSString *fileName = [[NSUserDefaults standardUserDefaults] objectForKey:@"recentDownload"];
NSString *fullPath = [NSBundle pathForResource:fileName ofType:@"txt" inDirectory:[NSHomeDirectory() stringByAppendingString:@"/Documents/"]];
NSError *error = nil;
[textViewerDownload setText:[NSString stringWithContentsOfFile:fullPath encoding: NSUTF8StringEncoding error:&error]];
是显示文件中文本的textviewerdownload
。实际文件名存储在名为textview
的recentDownload
中nsserdefault
- 当我构建它时,我单击下面的
,我的应用程序按钮
崩溃
- 语法有什么问题吗?或者只是一个简单的错误
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *fullPath = [[paths lastObject] stringByAppendingPathComponent:@"recentDownload.txt"];
这对我有用
无论如何,谢谢大家。从文本文件读取/写入请检查此项。好的,我更改了一些编码。。我的应用程序仍然崩溃。。下面是我的iAction下的代码:我如何执行if语句,如果最近的文件不是扩展名为“txt”的文件,它将返回,如果它是,则会导致一个操作。如果语法有问题,您的代码将无法编译;你将无法运行它并看到它崩溃。要调试崩溃,请使用调试器;它应该告诉您崩溃的方式和地点。请确保在info.plist中将布尔标志“UIFileSharingEnabled”设置为
YES
。
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPathDocs = [documentsDirectory stringByAppendingPathComponent:@"myfile.txt"];
if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
{
NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"txt"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:myPathInfo toPath:myPathDocs error:NULL];
}
//Load from File
NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];