gpt4 book ai didi

iphone - 获取文档目录中的目录数组

转载 作者:行者123 更新时间:2023-12-03 18:37:18 24 4
gpt4 key购买 nike

我正在尝试返回应用程序文档目录中存在的目录列表。我能够获取包含给定扩展名的所有文件(例如 .txt 文件或 .png 文件)的数组,并且能够返回所有内容(包括目录)。当我只想返回目录时,问题就出现了。有没有简单的方法可以做到这一点?

这是我返回所有 .txt 文件的代码:

- (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;
}

关于iphone - 获取文档目录中的目录数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2172245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com