NSCachesDirectory not a directory in my file syste

2019-03-27 15:20发布

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-49A9-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?

1条回答
Anthone
2楼-- · 2019-03-27 15:35

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

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;
}
查看更多
登录 后发表回答