iPhone - reducing file manager access
I have a metho开发者_Go百科d that deletes files. Actually I have this
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:myFile];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:appFile]) { //I am thinking about removing this line
[fileManager removeItemAtPath:appFile error:nil];
}
as I am trying to reduce the file manager access to a minimum, I am thinking in removing the line that checks for the existence of the file before removing it. Will it be safe? am I risking getting some kind of crash?
I have tested and I had no crash, but who knows... thanks
Yes, you can remove the check for the file to exist without a problem. You would normally check the return value of removeItemAtPath for YES/NO. It would return NO if the file didn't exist. At which point, you would normally check the NSError object for details.
精彩评论