4

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?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120
  • Not sure why this was down-voted. Legitimate question with content. +1. – ninehundreds Jun 09 '15 at 16:30
  • I'm experiencing almost-similar. It seems if I store as path, then convert to absolute URL, it fails. But if I store absolute string, then don't use fileURLWithPath, but instead urlWithName: it succeeds, thought it would interchange so long as I'm on the correct half of it – Stephen J Aug 10 '15 at 23:51

1 Answers1

7

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.

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • Not working... can you give me an example of what you are referring to when you say "just call path"? (taking my statement, what would it look like?) Sorry for the lame question, but I don't see how it would work... SD – SpokaneDude Feb 03 '15 at 00:37
  • @spokane-dude Instead of were you call `absoluteString`, call `path` instead. – JustSid Feb 03 '15 at 00:38
  • Thank you, thank you! That did it... I saw the *path* statement in some of the examples I was trying to follow, but there was nothing in the iOS docs where I could find the explaination... thanks again! – SpokaneDude Feb 03 '15 at 00:42
  • 1
    @spokane-dude Cheers! Just to clarify this, `absoluteString` simply returns the complete URL, but a a string. `path` merely returns the path of the URL, which doesn't contain things like protocol and the like. For file URLs, this is exactly what you want if you can't work the the file URL directly. – JustSid Feb 03 '15 at 00:44
  • Fantastic answer... I appreciate you taking the time to explain it to me. Cheers to you too! :D – SpokaneDude Feb 03 '15 at 00:51