获取文档目录中的目录数组 [英] Get array of directories in documents directory
本文介绍了获取文档目录中的目录数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试返回应用程序的Documents目录中的目录列表。我能够获得一个包含给定扩展名的所有文件的数组(例如.txt文件或.png文件),并且能够返回所有内容(包括目录)。当我想只返回目录时出现问题。有一个简单的方法吗?
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 .png files) and am able to return all contents (including directories). The problem arises when I want to return only the directories. Is there a simple way of doing this?
这是我的返回所有.txt文件的代码:
Here is my code for returning all .txt files:
- (ViewController *) init {
if (self = [super init]) self.title = @"Text Files";
// get the list of .txt files
directoryList = [[[[NSFileManager defaultManager] directoryContentsAtPath:DOCUMENTS_FOLDER]
pathsMatchingExtensions:[NSArray arrayWithObjects:@".txt", nil]] retain];
NSLog(@"%@", directoryList);
return self;
}
和所有文件
- (ViewController *) init {
if (self = [super init]) self.title = @"Text Files";
// get the list of all files and directories
directoryList = [[[NSFileManager defaultManager] directoryContentsAtPath:DOCUMENTS_FOLDER] retain];
NSLog(@"%@", directoryList);
return self;
}
推荐答案
试试这个:
- (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;
}
这篇关于获取文档目录中的目录数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文