iPhone - 减少文件管理器访问

标签 iphone cocoa nsfilemanager

我有一个删除文件的方法。其实我有这个

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];   
}

由于我试图将文件管理器的访问权限减少到最低限度,因此我正在考虑删除在删除文件之前检查文件是否存在的行。会安全吗?我是否有发生某种崩溃的风险?

我已经测试过了,没有发生崩溃,但谁知道呢...... 谢谢

最佳答案

是的,您可以毫无问题地取消对文件是否存在的检查。您通常会检查removeItemAtPath的返回值是/否。如果文件不存在,则返回 NO。此时,您通常会检查 NSError 对象以获取详细信息。

关于iPhone - 减少文件管理器访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4360179/

相关文章:

iphone - 调整模态视图的大小

iphone - 为 iPhone 创建 list (类似于 TouchCells 示例代码)应用程序时出现问题。检查随机细胞

Iphone:在哪里释放对象?

objective-c - NSSavePanel - 添加选择菜单

macos - isDirectory 参数是否为 +[NSURL fileURLWithPath :isDirectory:] need to be correct?

iphone - 对于大图像,UIImage imageNamed 是否仍然会导致 iOS 4 上的内存问题?

objective-c - stringByReplacingOccurrencesOfString 正则表达式

macos - 当光标位于 NSSearchField 上时显示消息

ios - 使用 URL 生成文件目录安全字符串?

ios - 如何检测iOS中大文件的存在?