Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a file exists objective-c

I used the codes below to check if a file exists

bool b=[[NSFileManager defaultManager] fileExistsAtPath:filePath];

The codes worked on IOS.

But When I migrate it to mac os x,

But I found whether the file exists on disk with filePath, b always returns 0, which means the file does not exists.

I wonder if there is difference between ios and macosx

Welcome any comment

like image 558
monsabre Avatar asked Oct 23 '11 14:10

monsabre


1 Answers

From docs, I think you're using path with tilde

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/fileExistsAtPath:isDirectory:

path

The path of a file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath, or this method will return NO.

tilde makes path relative to your home directory (such as /Users/username/)

you can find out by calling NSLog(@"%@",filePath); if filePath is of type NSString

like image 125
Marek Sebera Avatar answered Sep 19 '22 15:09

Marek Sebera