Search code examples
objective-ccocoa-touchnsdatansfilemanager

Difference between checking for a file's existence with NSData or NSFileManager


I'm trying to check if a file exists. If I use this:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:filePath]];

if (data == NULL){
   ...
}

I always get NULL.

Than I found NSFileManager:

NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:filePath])

works as I expect it to. What causes the difference between these two procedures?


Solution

  • What kind of file path are you passing to URLWithString? If it's a normal path (doesn't start with file://....), then you should be using fileURLWithPath: to convert a string path to a URL.