ios NSCachesDirectory 不是我的文件系统中的目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7014770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:02:43  来源:igfitidea点击:

NSCachesDirectory not a directory in my file system

iphoneiosobjective-cipadcaching

提问by user888382

I'm trying to store something in my Caches folder on my iPad app.

我正在尝试在 iPad 应用程序的 Caches 文件夹中存储一些内容。

    NSArray* cachePathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString* cachePath = [cachePathArray lastObject];

And when I print out the returned filepath, I get:

当我打印出返回的文件路径时,我得到:

/Users/Gela/Library/Application Support/iPhone Simulator/5.0/Applications/3FF7EB1A-49A9-4B13-ADC4-DF0662BA724B/Library/Caches

/Users/Gela/Library/Application Support/iPhone Simulator/5.0/Applications/3FF7EB1A-4​​9A9-4B13-ADC4-DF0662BA724B/Library/Caches

However, when I navigate to that folder on my hard drive, "Caches" is not a folder but a vague "document" file.

但是,当我导航到硬盘驱动器上的那个文件夹时,“缓存”不是一个文件夹,而是一个模糊的“文档”文件。

Any ideas why it's not a folder and how I can write to my Cache?

任何想法为什么它不是文件夹以及我如何写入我的缓存?

回答by Srikar Appalaraju

Maybe Simulatordoes not have Cachesdir. You try this on device...

也许Simulator没有Caches目录。你在设备上试试这个...

You can access the Cachesdirectory like this. I use this method for getting file data...

您可以Caches像这样访问目录。我使用这种方法来获取文件数据...

- (NSString *)getFileData: (NSString *)fileDirPath
{
    NSArray *myPathList = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *myPath    = [myPathList  objectAtIndex:0];
    NSError *err        = nil;
    NSString *fData     = @"";

    myPath = [myPath stringByAppendingPathComponent:fileDirPath];
    if([[NSFileManager defaultManager] fileExistsAtPath:myPath])
    {
        fData = [NSString stringWithContentsOfFile:myPath encoding:NSUTF8StringEncoding error:&err];
        if(err) NSLog(@"getFileData() - ERROR: %@",[err localizedDescription]);
    }
    else
    {
        NSLog(@"getFileData() - ERROR: This file '%@' does not exist",myPath);
    }
    return fData;
}