如何检查 Cocoa & Objective-C 中是否存在文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/99395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 20:45:10  来源:igfitidea点击:

How to check if a folder exists in Cocoa & Objective-C?

objective-ccocoa

提问by lajos

How to check if a folder (directory) exists in Cocoa using Objective-C?

如何使用Objective-C检查Cocoa中是否存在文件夹(目录)?

回答by Matt Dillard

Use NSFileManager's fileExistsAtPath:isDirectory:method. See Apple's docs here.

使用NSFileManagerfileExistsAtPath:isDirectory:方法。在此处查看 Apple 的文档。

回答by Ryan Bavetta

Some good advice from Apple in the NSFileManager.h regarding checking the file system:

苹果在 NSFileManager.h 中关于检查文件系统的一些很好的建议:

"It's far better to attempt an operation (like loading a file or creating a directory) and handle the error gracefully than it is to try to figure out ahead of time whether the operation will succeed. Attempting to predicate behavior based on the current state of the filesystem or a particular file on the filesystem is encouraging odd behavior in the face of filesystem race conditions."

“尝试一个操作(比如加载一个文件或创建一个目录)并优雅地处理错误比试图提前确定操作是否会成功要好得多。尝试根据当前状态来预测行为面对文件系统竞争条件,文件系统或文件系统上的特定文件鼓励奇怪的行为。”

回答by lajos

[NSFileManager fileExistsAtPath:isDirectory:]

[NSFileManager fileExistsAtPath:isDirectory:]

Returns a Boolean value that indicates whether a specified file exists.

- (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDirectory

Parameters
path
The path of a file or directory. If path begins with a tilde (~), it must first be expanded with stringByExpandingTildeInPath, or this method will return NO.

isDirectory
Upon return, contains YES if path is a directory or if the final path element is a symbolic link that points to a directory, otherwise contains NO. If path doesn't exist, the return value is undefined. Pass NULL if you do not need this information.

Return Value
YES if there is a file or directory at path, otherwise NO. If path specifies a symbolic link, this method traverses the link and returns YES or NO based on the existence of the file or directory at the link destination.

回答by Richard Stelling

NSFileManager is the best place to look for file related APIs. The specific API you require is - fileExistsAtPath:isDirectory:.

NSFileManager 是查找文件相关 API 的最佳位置。您需要的特定 API 是 - fileExistsAtPath:isDirectory:.

Example:

例子:

NSString *pathToFile = @"...";
BOOL isDir = NO;
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:pathToFile isDirectory:&isDir];

if(isFile)
{
    //it is a file, process it here how ever you like, check isDir to see if its a directory 
}
else
{
    //not a file, this is an error, handle it!
}

回答by Tong Liu

If your have a NSURLobject as path, it's better to use path to convert it into NSString.

如果您有一个NSURL对象 as path,最好使用 path 将其转换为NSString.

NSFileManager*fm = [NSFileManager defaultManager];

NSURL* path = [[[fm URLsForDirectory:NSDocumentDirectory 
                           inDomains:NSUserDomainMask] objectAtIndex:0]                           
                                 URLByAppendingPathComponent:@"photos"];

NSError *theError = nil;
if(![fm fileExistsAtPath:[path path]]){
    NSLog(@"dir doesn't exists");
}else
    NSLog(@"dir exists");