<分区>
标签 ios nsfilemanager
<分区>
我有以下代码返回给定目录中文件名的 NSArray
。当目录存在时这很好用,但我想知道如何首先检查 directoryPath 中的目录是否实际存在。
NSString *directoryPath = [documentsDirectory stringByAppendingPathComponent:folderName];
NSArray *directoryContents = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:directoryPath error:nil];
最佳答案
NSFileManager
类有一个内置方法,用于检查路径中是否存在文件。
函数是这样的:
[fileManagerObject fileExistsAtPath : @"PUT_YOUR_FILE_PATH_HERE" isDirectory : YES]; //If its a file that you're looking to check then put isDirectory as NO, but if its a folder then enter YES as the parameter
这是您通常在代码中使用它的方式:
NSFileManager *fileManager = [[NSFileManager alloc] init];
if([fileManager fileExistsAtPath:@"FILE_PATH_HERE" isDirectory:NO] == YES)
{
//Yes. The file exists, continue with your operation.
}
else
{
//No. The file doesn't exist.
}
关于ios - 如果目录不存在,使用 NSFileManager 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20089002/