gpt4 book ai didi

iphone - 如何在 iOS 中递归地获取我的包中的所有资源路径?

转载 作者:IT王子 更新时间:2023-10-29 08:18:20 25 4
gpt4 key购买 nike

我的应用程序包中有一个“解压缩”文件夹。我需要获取所有 txt 类型文件的资源路径。我一直在用这个,

NSArray *filePaths = [NSBundle pathsForResourcesOfType:@"txt" inDirectory:[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/unzipped"]];

但它只能获取第一个目录中的文件。是否有我刚刚缺少的递归版本?

最佳答案

我使用@rekle 发布的代码作为起点来完成这项工作。诀窍是使用 NSDirectoryEnumerator,它将递归地执行此操作。这是我编写的函数,以备不时之需。

- (NSArray *)recursivePathsForResourcesOfType:(NSString *)type inDirectory:(NSString *)directoryPath{

NSMutableArray *filePaths = [[NSMutableArray alloc] init];

// Enumerators are recursive
NSDirectoryEnumerator *enumerator = [[[NSFileManager defaultManager] enumeratorAtPath:directoryPath] retain];

NSString *filePath;

while ((filePath = [enumerator nextObject]) != nil){

// If we have the right type of file, add it to the list
// Make sure to prepend the directory path
if([[filePath pathExtension] isEqualToString:type]){
[filePaths addObject:[directoryPath stringByAppendingPathComponent:filePath]];
}
}

[enumerator release];

return [filePaths autorelease];
}

Swift,使用 NSURL

func recursivePathsForResources(type type: String) -> [NSURL] {

// Enumerators are recursive
let enumerator = NSFileManager.defaultManager().enumeratorAtPath(bundlePath)
var filePaths = [NSURL]()

while let filePath = enumerator?.nextObject() as? String {

if NSURL(fileURLWithPath: filePath).pathExtension == type {
filePaths.append(bundleURL.URLByAppendingPathComponent(filePath))
}
}

return filePaths
}

关于iphone - 如何在 iOS 中递归地获取我的包中的所有资源路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5836587/

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