【问题标题】:how to find subfolder in an existing folder in iOS如何在iOS中的现有文件夹中查找子文件夹
【发布时间】:2014-08-22 01:43:23
【问题描述】:

我的文件夹只包含一个子文件夹,我不知道子文件夹的名称。这个子文件夹包含一个 html 文件,我又一次不知道 html 文件的名称。

我的问题是如何使用

获取此文件的完整路径 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *folderPath = [documentsDirectory stringByAppendingPathComponent:filename]; //- 访问子文件夹? //- 访问 html 文件?

编辑:

我写了一个方法来返回唯一的一个子文件夹如下:

+ (NSString*) get1stSubFolder:(NSString*) 文件夹 { NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:folder]; //- 没有递归 [directoryEnumerator skipDescendents]; NSString* 文件; while (file = [directoryEnumerator nextObject]) { BOOL isDirectory = 否; BOOL subFileExists = [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory]; if (subFileExists && !isDirectory) { 返回文件; } } 返回零; }

我总是得到 nil 结果。你知道我哪里错了吗?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    使用NSDirectoryEnumerator。它应该对你有帮助。

    【讨论】:

      【解决方案2】:

      像这样使用 NSDirectoryEnumerator。

      NSURL *documentsDirectoryURL = [NSURL URLWithString:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] ];
      ///If the folder you want to browse for subfolders is NSDocumentDirectory.
      
      NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];
      
      NSDirectoryEnumerator *enumerator = [[[NSFileManager alloc] init]
                                           enumeratorAtURL:documentsDirectoryURL
                                           includingPropertiesForKeys:keys
                                           options:0
                                           errorHandler:^(NSURL *url, NSError *error) {
                                               return YES;
                                           }];
      
       for (NSURL *url in enumerator) {
          NSError *error;
          NSNumber *isDirectory = nil;
          if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
              NSLog(@"Error %@",error);
          }
          else if ([isDirectory boolValue]) {
              NSLog(@"Folder URL: %@",url);
          }else{
              NSLog(@"File URL: %@",url);
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        相关资源
        最近更新 更多