Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't NSFIleManager -fileExistsAtPath not find an existing file when path is correct?

I know that the iOS Simulator is found in a different directory each time it is run; with that in mind, I have this code which gives me the directory of the Core Data sqlite files:

//  find current directory for saori.sqlite
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentDirectory = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]firstObject];
NSString *sqliteFilePath  = [[documentDirectory URLByAppendingPathComponent:@"Application Support/SalonBook/saori.sqlite"] absoluteString];
if ([fileManager fileExistsAtPath:sqliteFilePath])
    [MagicalRecord cleanUp];  //  set stack, etc to 'nil'
else  {
    NSLog(@"\n\n-->sqlite files not found");  // log message "unable to find sqlite files
    return;
}

This is the printout of the sqliteFilePath object:

Printing description of sqliteFilePath:
file:///Users/rolfmarsh/Library/Developer/CoreSimulator/Devices/1EE69744-255A-45CD-88F1-63FEAD117B32/data/Containers/Data/Application/C8FF20F0-41E4-4F26-AB06-1F29936C2208/Library/Application%20Support/SalonBook/saori.sqlite

And this is the image of the file from Finder: enter image description here

The problem is: I go to the sqliteFilePath and the saori.sqlite file is indeed there! Why is -fileExistsAtPath failing?

like image 223
SpokaneDude Avatar asked Oct 15 '25 18:10

SpokaneDude


1 Answers

Because it is still a URL. A file path doesn't have a protocol, so the prefix of your path file:/// is invalid and can't be resolved. Since an invalid path doesn't contain any files, fileExistsAtPath: returns NO.

Not to worry though, instead of calling absoluteString on the URL object, you can just call path instead and it will return the path.

like image 158
JustSid Avatar answered Oct 18 '25 20:10

JustSid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!