Get array of directories in documents directory

后端 未结 2 448
囚心锁ツ
囚心锁ツ 2021-02-06 09:45

I am trying to return the list of directories present in an app\'s Documents directory. I am able to get an array containing all files of a given extension (eg .txt files or .pn

相关标签:
2条回答
  • 2021-02-06 10:25

    Try this:

    - (ViewController *) init {
        if (self = [super init]) self.title = @"Text Files";
    
        // get the list of all files and directories
        NSFileManager *fM = [NSFileManager defaultManager];
        fileList = [[fM directoryContentsAtPath:DOCUMENTS_FOLDER] retain];
        NSMutableArray *directoryList = [[NSMutableArray alloc] init];
        for(NSString *file in fileList) {
            NSString *path = [DOCUMENTS_FOLDER stringByAppendingPathComponent:file];
            BOOL isDir = NO;
            [fM fileExistsAtPath:path isDirectory:(&isDir)];
            if(isDir) {
                [directoryList addObject:file];
            }
        }
    
        NSLog(@"%@", directoryList);
    
        return self;
    }
    
    0 讨论(0)
  • 2021-02-06 10:29

    Mrueg wrote it all but one thing that

    fileList = [[fM directoryContentsAtPath:DOCUMENTS_FOLDER];
    

    Replace by

    fileList = [fileManager contentsOfDirectoryAtPath:filePath error:nil];
    
    0 讨论(0)
提交回复
热议问题