NSCachesDirectory不是我文件系统中的目录 [英] NSCachesDirectory not a directory in my file system

查看:104
本文介绍了NSCachesDirectory不是我文件系统中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将某些内容存储在我的i​​Pad应用上的Caches文件夹中。

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

    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-4​​9A9-4B13-ADC4-DF0662BA724B / Library / Caches

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

然而,当我导航到硬盘上的那个文件夹时,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?

推荐答案

可能模拟器没有缓存目录。你在设备上试试这个......

Maybe Simulator does not have Caches dir. You try this on device...

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

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

- (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;
}

这篇关于NSCachesDirectory不是我文件系统中的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆