为什么NSFileManager无法打开文件?

时间:2012-04-03 08:00:50

标签: iphone objective-c ios nsfilemanager

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!!!");

这是上面代码生成的日志:

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!!!

为什么我在这里收到错误。我的意思是为什么目录不存在?

1 个答案:

答案 0 :(得分:5)

fileExistsAtPath:需要一个路径作为参数,但是您将URL设为absoluteString。

更改

NSString* directory = [url absoluteString];

NSString* directory = [url path];