获取 Resources 文件夹中的文件列表 - iOS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6398937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a list of files in the Resources folder - iOS
提问by CodeGuy
Let's say I have a folder in my "Resources" folder of my iPhone application called "Documents".
假设我的 iPhone 应用程序的“资源”文件夹中有一个名为“文档”的文件夹。
Is there a way that I can get an array or some type of list of all the files included in that folder at run time?
有没有办法可以在运行时获取该文件夹中包含的所有文件的数组或某种类型的列表?
So, in code, it would look like:
所以,在代码中,它看起来像:
NSMutableArray *myFiles = [...get a list of files in Resources/Documents...];
Is this possible?
这可能吗?
回答by Deepak Danduprolu
You can get the path to the Resources
directory like this,
您可以Resources
像这样获取目录的路径,
NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
Then append the Documents
to the path,
然后将 附加Documents
到路径,
NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"];
Then you can use any of the directory listing APIs of NSFileManager
.
然后,您可以使用NSFileManager
.
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];
Note: When adding source folder into the bundle make sure you select "Create folder references for any added folders option when copying"
注意:将源文件夹添加到包中时,请确保选择“复制时为任何添加的文件夹创建文件夹引用选项”
回答by Suragch
Swift
迅速
Updated for Swift 3
为 Swift 3 更新
let docsPath = Bundle.main.resourcePath! + "/Resources"
let fileManager = FileManager.default
do {
let docsArray = try fileManager.contentsOfDirectory(atPath: docsPath)
} catch {
print(error)
}
Further reading:
进一步阅读:
回答by neowinston
You can try this code also:
你也可以试试这个代码:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:documentsDirectory error:&error];
NSLog(@"directoryContents ====== %@",directoryContents);
回答by Matt Frear
Swift version:
迅捷版:
if let files = try? FileManager.default.contentsOfDirectory(atPath: Bundle.main.bundlePath ){
for file in files {
print(file)
}
}
回答by Govind
Listing All Files In A Directory
列出目录中的所有文件
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSArray *contents = [fileManager contentsOfDirectoryAtURL:bundleURL
includingPropertiesForKeys:@[]
options:NSDirectoryEnumerationSkipsHiddenFiles
error:nil];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"pathExtension ENDSWITH '.png'"];
for (NSString *path in [contents filteredArrayUsingPredicate:predicate]) {
// Enumerate each .png file in directory
}
Recursively Enumerating Files In A Directory
递归枚举目录中的文件
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *bundleURL = [[NSBundle mainBundle] bundleURL];
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtURL:bundleURL
includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:^BOOL(NSURL *url, NSError *error)
{
NSLog(@"[Error] %@ (%@)", error, url);
}];
NSMutableArray *mutableFileURLs = [NSMutableArray array];
for (NSURL *fileURL in enumerator) {
NSString *filename;
[fileURL getResourceValue:&filename forKey:NSURLNameKey error:nil];
NSNumber *isDirectory;
[fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
// Skip directories with '_' prefix, for example
if ([filename hasPrefix:@"_"] && [isDirectory boolValue]) {
[enumerator skipDescendants];
continue;
}
if (![isDirectory boolValue]) {
[mutableFileURLs addObject:fileURL];
}
}
For more about NSFileManager its here
有关 NSFileManager 的更多信息,请点击此处
回答by narco
Swift 3(and returning URLs)
Swift 3(和返回的 URL)
let url = Bundle.main.resourceURL!
do {
let urls = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys:[], options: FileManager.DirectoryEnumerationOptions.skipsHiddenFiles)
} catch {
print(error)
}
回答by Alessandro Ornano
Swift 4:
斯威夫特 4:
If you have to do with subdirs "Relative to project"(blue folders) you could write:
如果您必须处理子目录“相对于项目”(蓝色文件夹),您可以编写:
func getAllPListFrom(_ subdir:String)->[URL]? {
guard let fURL = Bundle.main.urls(forResourcesWithExtension: "plist", subdirectory: subdir) else { return nil }
return fURL
}
Usage:
用法:
if let myURLs = getAllPListFrom("myPrivateFolder/Lists") {
// your code..
}