Search code examples
iphoneobjective-ciosnsfilemanager

Why can't NSFileManager open file?


NSFileManager* fileManager = [NSFileManager defaultManager];
NSURL* url = [[fileManager URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
NSLog(@"%@",url);
NSString* directory = [url absoluteString];
NSLog(@"%@",directory);
if (![fileManager fileExistsAtPath:directory]) NSLog(@"error!!!");

Here's the log generated by the code above:

2012-04-03 15:45:02.298 TopPlaces[805:13303] file://localhost/Users/yzyoyosir/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/2D6EC144-CF85-4994-8904-8FF5F8407FED/Library/Caches/
2012-04-03 15:45:02.300 TopPlaces[805:13303] file://localhost/Users/yzyoyosir/Library/Application%20Support/iPhone%20Simulator/5.0/Applications/2D6EC144-CF85-4994-8904-8FF5F8407FED/Library/Caches/
2012-04-03 15:45:02.301 TopPlaces[805:13303] error!!!

Why have I got an error here. I mean why the directory does not exist?


Solution

  • fileExistsAtPath: needs a path as argument but you're giving an URL as absoluteString.

    Change

    NSString* directory = [url absoluteString];
    

    to

    NSString* directory = [url path];