iOS之判断路径是文件还是文件夹

iOS之判断路径是文件还是文件夹,第1张

[NSFileManager defaultManager]有这么一个api

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

isDirectory是一个指针

说明如下:

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

就是说传入一个bool类型的指针,执行改方法后这个参数的值是yes的话就是路径,反之是文件。

用法如下:

BOOL isDir = NO

        [[NSFileManager defaultManager] fileExistsAtPath:allPath isDirectory:&isDir]

        if(isDir)//是文件夹

        {

}

- (void)createFileDirectories

{

// 判断存放音频、视频的文件夹是否存在,不存在则创建对应文件夹

NSFileManager *fileManager = [NSFileManagerdefaultManager]

BOOL isDir = FALSE

BOOL isDirExist = [fileManager fileExistsAtPath:DOCUMENTS_FOLDER_AUDIOisDirectory:&isDir]

if(!(isDirExist &&isDir))

{

BOOL bCreateDir = [fileManager createDirectoryAtPath:DOCUMENTS_FOLDER_AUDIOwithIntermediateDirectories:YESattributes:nilerror:nil]

if(!bCreateDir){

NSLog(@"Create Audio Directory Failed.")

}

NSLog(@"%@",DOCUMENTS_FOLDER_AUDIO)

}

isDir = FALSE

isDirExist = [fileManager fileExistsAtPath:DOCUMENTS_FOLDER_VEDIOisDirectory:&isDir]

if(!(isDirExist &&isDir)){

BOOL bCreateDir = [fileManager createDirectoryAtPath:DOCUMENTS_FOLDER_VEDIOwithIntermediateDirectories:YESattributes:nilerror:nil]

if(!bCreateDir){

NSLog(@"Create Audio Directory Failed.")

}

NSLog(@"%@",DOCUMENTS_FOLDER_VEDIO)

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12001193.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存