Ios 使用NSFileManager在目录未退出时返回
下面的代码返回给定目录中文件名的Ios 使用NSFileManager在目录未退出时返回,ios,nsfilemanager,Ios,Nsfilemanager,下面的代码返回给定目录中文件名的NSArray。当目录存在时,这可以正常工作,但我想知道的是如何首先检查directoryPath上的目录是否确实存在 NSString *directoryPath = [documentsDirectory stringByAppendingPathComponent:folderName]; NSArray *directoryContents = [[NSFileManager defaultManager]contentsOfDirectoryAtPat
NSArray
。当目录存在时,这可以正常工作,但我想知道的是如何首先检查directoryPath上的目录是否确实存在
NSString *directoryPath = [documentsDirectory stringByAppendingPathComponent:folderName];
NSArray *directoryContents = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:directoryPath error:nil];
使用NSFileManager的fileExistsAtPath:isDirectory:方法。使用NSFileManager的fileExistsAtPath:isDirectory:方法
fileExistsAtPath:isDirectory:
返回值
如果指定路径上的文件存在,则为“是”;如果文件不存在或无法确定其存在,则为“否”
返回值
如果指定路径上的文件存在,则为“是”;如果文件不存在或无法确定其存在,则为“否”
以下NSFileManger API可以提供所需的结果:
以下NSFileManger API可以提供所需的结果: 使用以下方法:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocumentsDir = [paths objectAtIndex:0];
BOOL isDir;
NSString *subfolder = [pathToDocumentsDir stringByAppendingPathComponent:fileExistsAtPath:YOUR_DIRECTORY_NAME];
if (![fileManager fileExistsAtPath:subfolder isDirectory:&isDir]) {
//No directory exist
[fileManager createDirectoryAtPath:subfolder withIntermediateDirectories:NO attributes:nil error:nil];
}
else{
//directory exist
}
使用以下命令:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *pathToDocumentsDir = [paths objectAtIndex:0];
BOOL isDir;
NSString *subfolder = [pathToDocumentsDir stringByAppendingPathComponent:fileExistsAtPath:YOUR_DIRECTORY_NAME];
if (![fileManager fileExistsAtPath:subfolder isDirectory:&isDir]) {
//No directory exist
[fileManager createDirectoryAtPath:subfolder withIntermediateDirectories:NO attributes:nil error:nil];
}
else{
//directory exist
}
示例代码:
NSString *directoryPath = [documentsDirectory stringByAppendingPathComponent:folderName];
BOOL isDirectory;
BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (isExists) {
/* file exists */
if (isDirectory) {
/* file is a directory */
}
}
示例代码:
NSString *directoryPath = [documentsDirectory stringByAppendingPathComponent:folderName];
BOOL isDirectory;
BOOL isExists = [[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:&isDirectory];
if (isExists) {
/* file exists */
if (isDirectory) {
/* file is a directory */
}
}
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.
}
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.
}
谢谢-感谢你的帮助谢谢-感谢你的帮助谢谢你的帮助。我已经添加了来自Sanjeet的代码,它似乎有效,所以我不确定你的评论是什么意思。你说你想检查目录是否存在,他正在检查文件。谢谢你的帮助。我已经添加了来自Sanjeet的代码,它似乎起作用了,所以我不确定你的评论是什么意思。你说你想检查目录是否存在,他正在检查文件。