Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/94.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 读取文档文件夹中的文本文件-Iphone SDK_Ios_Iphone_Objective C_Cocoa_Documents - Fatal编程技术网

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

  • 当我构建它时,我单击下面的
    按钮
    ,我的应用程序
    崩溃

  • 语法有什么问题吗?或者只是一个简单的错误


NSBundle类用于查找应用程序捆绑包中的内容,但文档目录位于捆绑包之外,因此生成路径的方式不起作用。请尝试以下方法:

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];