简体   繁体   中英

Getting an array of items in documents directory iOS

I have some folders and subfolders in my documents directory how can i get an array with the files in the directories and subdirectories not the folders just the files.

here is my code so far and it's working if no folders and subfolders exist

//getting all the files on documents folder in an array.
    NSError * error;
    NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:documentsDirectory];
NSMutableArray *files = [NSMutableArray array];
BOOL isDirectory;
NSString *path;
while (path = [enumerator nextObject]) {
    if ([fileManager fileExistsAtPath:path isDirectory:&isDirectory] && !isDirectory) {
        [files addObject:path];
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM