Search code examples
nsurlnsfilemanager

NSFileManager - fileExistsAtPath:?


I am trying to understand how to use NSFileManager, but I have a file that does exist on my desktop and the NSFileManager method fileExistsAtath: is returning False when I enter the path of that file. Seems I am missing something. Here is the code.

NSFileManager *fileManager = [NSFileManager defaultManager];

//self.pathToRead is /Users/nikita/Desktop/asdf.wav
NSString *readPath = [[NSURL fileURLWithPath:[self.pathToRead stringValue]] absoluteString];

//readPath is file:///Users/nikita/Desktop/asdf.wav
if(![fileManager fileExistsAtPath:readPath])
   NSLog(@"File does not exist!");

Solution

  • Change:

    NSString *readPath = [[NSURL fileURLWithPath:[self.pathToRead stringValue]] absoluteString];
    

    To

    NSString *readPath = [[NSURL fileURLWithPath:[self.pathToRead stringValue]] path];