【问题标题】:Using NSFileManager to return if directory does not exit [duplicate]如果目录不退出,则使用 NSFileManager 返回 [重复]
【发布时间】:2013-12-04 00:51:58
【问题描述】:

我有以下代码返回给定目录中文件名的NSArray。当目录存在时这很好用,但我想知道的是如何首先检查 directoryPath 处的目录是否实际存在。

NSString *directoryPath = [documentsDirectory stringByAppendingPathComponent:folderName];
NSArray *directoryContents = [[NSFileManager defaultManager]contentsOfDirectoryAtPath:directoryPath error:nil];

【问题讨论】:

    标签: ios nsfilemanager


    【解决方案1】:

    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.
    }
    

    【讨论】:

      【解决方案2】:

      使用 NSFileManager 的 fileExistsAtPath:isDirectory: 方法。

      【讨论】:

        【解决方案3】:
        fileExistsAtPath:isDirectory:
        

        返回值 如果指定路径的文件存在,则为 YES,如果文件不存在或无法确定其存在,则为 NO。

        Apple Documentation for NSFileManager

        【讨论】:

        • 感谢您的帮助。我已经添加了 Sanjeet 的代码,它似乎可以工作,所以我不确定你的评论是什么意思。
        • 你说你要检查目录是否存在,他在这里检查文件。
        【解决方案4】:

        按照 NSFileManger API 可以得到想要的结果:

        fileExistsAtPath:isDirectory:

        【讨论】:

          【解决方案5】:

          使用这个:

              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
          }
          

          【讨论】:

          • 谢谢 - 感谢您的帮助
          【解决方案6】:

          示例代码:

          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 */
          
              }
           }
          

          【讨论】:

            猜你喜欢
            • 2012-05-09
            • 2014-02-04
            • 2021-05-05
            • 2019-04-25
            • 1970-01-01
            • 1970-01-01
            • 2013-08-04
            • 1970-01-01
            • 2016-07-29
            相关资源
            最近更新 更多