我如何获得我的应用程序中给定目录中的所有文件的文件名NSArray? [英] How do I get an NSArray of filenames of all files in a given directory in my app?

查看:132
本文介绍了我如何获得我的应用程序中给定目录中的所有文件的文件名NSArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的事似乎很简单:在我的应用程序的给定目录中获取一个文件名数组。但是,我玩的NSFileManager和NSBundle越多,我发现自己越来越迷路...我只想得到在我的iPhone Xcode项目中的特定目录中组织的文件的文件名...例如:有问题的目录是所谓的图像(使用Xcode中项目图标下的添加>新建组)创建,并包含10个.png图像。这看起来很简单,但我没有办法。



Q:如何在我的应用中获得给定目录下所有文件的文件名NSArray?如果你想让这个文件夹成为应用程序包的一部分,你需要在里面创建一个文件夹引用。 资源组,而不是一个组(它将在Xcode中显示为蓝色文件夹,而不是黄色文件夹)。

为此,请拖动文件夹从Finder中选择,然后在出现提示时选择文件夹引用。



一旦你有了这些,你应该能够得到类似于文件夹的内容:


$ b

  NSError * error = nil; 

NSString * yourFolderPath = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:@YourFolder];

NSArray * yourFolderContents = [[NSFileManager defaultManager]
contentsOfDirectoryAtPath:yourFolderPath error:& error];


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.

Q: How do I get an NSArray of filenames of all files in a given directory in my app?

解决方案

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).

To do this, you drag the folder from Finder and select folder reference when prompted.

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];

这篇关于我如何获得我的应用程序中给定目录中的所有文件的文件名NSArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆