Getting a list of files in the Resources folder - iOS

前端 未结 7 2228
悲哀的现实
悲哀的现实 2020-11-28 19:52

Let\'s say I have a folder in my \"Resources\" folder of my iPhone application called \"Documents\".

Is there a way that I can get an array or some type of list of a

7条回答
  •  死守一世寂寞
    2020-11-28 20:33

    Listing All Files In A Directory

         NSFileManager *fileManager = [NSFileManager defaultManager];
         NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
         NSArray *contents = [fileManager contentsOfDirectoryAtURL:bundleURL
                               includingPropertiesForKeys:@[]
                                                  options:NSDirectoryEnumerationSkipsHiddenFiles
                                                    error:nil];
    
         NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension ENDSWITH '.png'"];
         for (NSString *path in [contents filteredArrayUsingPredicate:predicate]) {
            // Enumerate each .png file in directory
         }
    

    Recursively Enumerating Files In A Directory

          NSFileManager *fileManager = [NSFileManager defaultManager];
          NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
          NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:bundleURL
                                       includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
                                                         options:NSDirectoryEnumerationSkipsHiddenFiles
                                                    errorHandler:^BOOL(NSURL *url, NSError *error)
          {
             NSLog(@"[Error] %@ (%@)", error, url);
          }];
    
          NSMutableArray *mutableFileURLs = [NSMutableArray array];
          for (NSURL *fileURL in enumerator) {
          NSString *filename;
          [fileURL getResourceValue:&filename forKey:NSURLNameKey error:nil];
    
          NSNumber *isDirectory;
          [fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
    
           // Skip directories with '_' prefix, for example
          if ([filename hasPrefix:@"_"] && [isDirectory boolValue]) {
             [enumerator skipDescendants];
             continue;
           }
    
          if (![isDirectory boolValue]) {
              [mutableFileURLs addObject:fileURL];
           }
         }
    

    For more about NSFileManager its here

提交回复
热议问题