iPhone - 减少文件管理器访问
我有一个删除文件的方法。实际上,我有这个
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];
}
,因为我试图将文件管理器访问减少到最低限度,我正在考虑删除在删除文件之前检查文件是否存在的行。会安全吗?我是否有发生某种事故的风险?
我测试过,没有崩溃,但谁知道呢... 谢谢
I have a method 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您可以毫无问题地删除对文件是否存在的检查。您通常会检查removeItemAtPath的返回值是/否。如果文件不存在,则返回 NO。此时,您通常会检查 NSError 对象以获取详细信息。
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.