简体   繁体   English

NSFileManager,如何确定文件或目录是否可以删除?

[英]NSFileManager, how to determine if a file or directory can be removed?

Is there a way to tell, if calling 有没有办法告诉,如果打电话

[[NSFileManager defaultManager] removeItemAtPath:filePath error:NULL];

will fail? 将失败?

I think you want to check out NSFileManager's -isDeletableFileAtPath: which works for both files and directories https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html#//apple_ref/occ/instm/NSFileManager/isDeletableFileAtPath : 我认为您想查看NSFileManager的-isDeletableFileAtPath:既适用于文件又适用于目录https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html# // apple_ref / occ / instm / NSFileManager / isDeletableFileAtPath

However, you should probably heed the note listed in the documentation: 但是,您可能应该注意文档中列出的注释:

Note: Attempting to predicate behavior based on the current state of the file system or a particular file on the file system is not recommended. 注意:不建议尝试基于文件系统的当前状态或文件系统上的特定文件来断言行为。 Doing so can cause odd behavior or race conditions. 这样做可能会导致奇怪的行为或比赛条件。 It's far better to attempt an operation (such as loading a file or creating a directory), check for errors, and handle those errors gracefully than it is to try to figure out ahead of time whether the operation will succeed. 尝试执行某个操作(例如加载文件或创建目录),检查错误并优雅地处理这些错误比尝试提前确定该操作是否成功要好得多。 For more information on file system race conditions, see “Race Conditions and Secure File Operations” in Secure Coding Guide. 有关文件系统竞争条件的更多信息,请参阅《安全编码指南》中的“种族条件和安全文件操作”。

Try to remove it and check for errors instead of passing NULL 尝试将其删除并检查错误,而不是传递NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM