iPhone - 减少文件管理器访问

发布于 2024-10-06 10:59:44 字数 602 浏览 2 评论 0原文

我有一个删除文件的方法。实际上,我有这个

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的奇迹 2024-10-13 10:59:44

是的,您可以毫无问题地删除对文件是否存在的检查。您通常会检查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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文