Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCachesDirectory not a directory in my file system

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?

like image 531
user888382 Avatar asked Aug 10 '11 16:08

user888382


1 Answers

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;
}
like image 84
Srikar Appalaraju Avatar answered Sep 21 '22 10:09

Srikar Appalaraju