Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fileExistsAtPath: returning NO for files that exist

Tags:

ios

At a point in my code fileExistsAtPath: is returning NO for files that I have confirmed exist. I've been scratching my head at this and can't figure out why its not working, so changed it to this code as this directory absolutely exists but if it doesn't gets created anyway.

NSError* err = nil; NSURL *dir = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory                                                        inDomain:NSUserDomainMask                                               appropriateForURL:nil                                                             create: YES                                                           error:&err]; BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:[dir absoluteString]]; 

After running this code the application directory folder exists and err is 0, yet exists is NO.

How can this be?

TIA

like image 828
Gruntcakes Avatar asked Feb 16 '12 00:02

Gruntcakes


Video Answer


2 Answers

You should use [dir path], not [dir absoluteString].

like image 170
bneely Avatar answered Sep 30 '22 18:09

bneely


I was bashing my head against the wall for a few hours. Apparently on each and every run in xcode (on simulator) the app directory path was changing. The UUID part of it. So instead of storing the fullpath I ended up persisting the path postfix and prefixing that with whatever storage class is implied: temporary, cached or documents :-[ You realize why you have to sometimes run on device even if you don't explore the depths of Metal, GLES or multitouch? ;^)

like image 37
Anton Tropashko Avatar answered Sep 30 '22 17:09

Anton Tropashko