hi all, i am new to iPhone programming. What is the proper way to check that whether a file is exist or not?
+4
A:
BOOL isDirectory = NO;
if ( [[NSFileManager defaultManager]
fileExistsAtPath:path
isDirectory: &isDirectory )
EricS
2009-09-29 14:01:54
Can not be much more simple than this. This is a good showcase of my own rule of thumb: _"If I have written more then 10 lines of code, then I have probably missed a method already provided by Cocoa"_.
PeyloW
2009-09-29 15:08:45
+3
A:
To put it in more detail:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([[NSFileManager defaultManager] fileExistsAtPath:documentsDirectory]){
//Do something...
}
You can append the actual file name to "documentsDirectory" like this: [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", spidermanpic]].
The isDirectory option in the answer above is used to check if the path is a directory or a file. Please keep in mind that it is a pointer. It wont work without the "&".
Chintan Patel
2009-09-29 14:37:10