ios - 获取存储在应用程序文档文件夹中的文件内容

标签 ios objective-c xcode

我想在我的应用程序文档文件夹中获取文件的内容。所以我写了下面的代码。但是这段代码没有在我的应用程序文档文件夹中获取文件内容。在我的文档文件夹中包含我需要的文件。

- (void) displayContents
{
    NSString *folderName = [@"Object File/" stringByAppendingString:strLastpath];
    NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docsDir = [dirPaths objectAtIndex:0];
    databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent:folderName]];
    NSError *theError = nil;

    NSFileManager *filemgr;
    NSArray *filelist;
    int count;
    int i;

    filemgr =[NSFileManager defaultManager];
    filelist = [filemgr contentsOfDirectoryAtPath:databasePath error:&theError];
    count = [filelist count];

    for (i = 0; i < count; i++)
    {
        NSLog(@"%@", filelist[i]);
    }

}

我得到的错误是:

2013-10-14 11:18:55.680 SampleFileCreation[1599:c07] Database Path : /Users/mac/Library/Application Support/iPhone Simulator/6.1/Applications/3B43DFF3-6699-4D68-94A6-A5153F3B1404/Documents/Object File/banana.obj
2013-10-14 11:18:55.683 SampleFileCreation[1599:c07] File Exists
2013-10-14 11:18:55.685 SampleFileCreation[1599:c07] Error : The operation couldn’t be completed. (Cocoa error 260.)

最佳答案

Cocoa 错误 260 是一个 NSFileReadNoSuchFileError,表示无法在您指定的路径找到该文件。

问题是您的路径仍然包含编码空格 (%20),因为您将它基于 URL。请将 URL 与路径一起使用。

关于ios - 获取存储在应用程序文档文件夹中的文件内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19354223/

相关文章:

ios - uicollectionview 加载异常

objective-c - 如何在宏中使用 block

ios - Storyboard启动检查的最佳实践

ios - 我可以将 XCUITest 与 swift 一起用于使用 Objective C 开发的 iOS 应用程序的 UI 自动化吗

ios - 自动布局违反平移旋转动画

ios - 自定义字体不适用于尺寸等级

ios - TableView reloadData 与 beginUpdates & endUpdates

ios - 更改 NSTextAttachment 图像的不透明度

iphone - 如何知道为什么应用程序在模拟器中崩溃

iphone - 显示 NSArray 中的每个对象 2 秒