xcode 如何在我的应用程序中获取给定目录中所有文件的文件名的 NSArray?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1272042/
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
How do I get an NSArray of filenames of all files in a given directory in my app?
提问by RexOnRoids
What I want to do seems simple enough: Get an array of filenames in a given "directory" on my app. But the more I play around with NSFileManager and NSBundle I find myself getting more lost...I just want to get the filenames of the files organized in a specific directory in my iPhone Xcode project...For example: The directory in question is called "Images" (created using "Add > New Group" under the project icon in Xcode) and contains 10 .png images. It seems simple but I am getting nowhere. I'd appreciate some help in finding the answer.
我想要做的似乎很简单:在我的应用程序的给定“目录”中获取文件名数组。但是我越玩 NSFileManager 和 NSBundle 我发现自己越来越迷失......我只想在我的 iPhone Xcode 项目的特定目录中组织文件的文件名......例如:有问题的目录是称为“图像”(使用 Xcode 中项目图标下的“添加 > 新组”创建)并包含 10 个 .png 图像。这看起来很简单,但我无处可去。我很感激找到答案的一些帮助。
Q: How do I get an NSArray of filenames of all files in a given directory in my app?
问:如何在我的应用程序中获取给定目录中所有文件的文件名的 NSArray?
回答by pgb
If you want the folder to be part of the App bundle, you need to create a folder reference inside the Resources group, instead of a group (it will show up in Xcode as a blue folder as opposed to the yellow one).
如果您希望该文件夹成为 App 包的一部分,您需要在 Resources 组内创建一个文件夹引用,而不是一个组(它将在 Xcode 中显示为蓝色文件夹而不是黄色文件夹)。
To do this, you drag the folder from Finder and select folder reference when prompted.
为此,您可以从 Finder 中拖动文件夹,并在出现提示时选择文件夹引用。
Once you have that, you should be able to get the contents of the folder with something like:
完成后,您应该能够使用以下内容获取文件夹的内容:
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@"YourFolder"];
NSArray *yourFolderContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:yourFolderPath error:&error];
回答by james_womack
The accepted answer's been deprecated for years now. The following should be used instead.
多年来,已接受的答案已被弃用。应改为使用以下内容。
- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
Update: @macserv updated the accepted answer after I posted this.
更新:@macserv 在我发布后更新了接受的答案。
回答by jmoukel
Nowadays, this is how you do it:
如今,这就是你的做法:
- From the Finder, Drag & Drop your folder in your project's navigator and the next popup will show up:
- As you can see in that screenshot, you have to select Create folder references.
Do what the solution for this post is tells you to do. I mean:
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YourFolder"];
NSArray *yourFolderContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:yourFolderPath error:&error];
- 从 Finder 中,将您的文件夹拖放到项目的导航器中,然后将显示下一个弹出窗口:
- 正如您在该屏幕截图中看到的,您必须选择Create folder references。
做这篇文章的解决方案告诉你要做的事情。我的意思是:
NSError *error = nil;
NSString *yourFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"YourFolder"];
NSArray *yourFolderContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:yourFolderPath 错误:&error];