简体   繁体   中英

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?

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

Change

NSString* directory = [url absoluteString];

to

NSString* directory = [url path];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM