您可以根据文件夹内容构建项目数组吗?

发布于 2024-09-10 12:29:02 字数 219 浏览 2 评论 0原文

假设我的项目中有一个文件夹,其中包含一堆 png 文件(或 mp3 文件或 mov 文件等)。

我想创建一个 NSArray,填充该文件夹中的项目...例如图像文件夹的 UIImages 数组(或者只是图像名称的 NSStrings 数组)。

这可能吗?我知道我可以创建一个包含所有名称的 plist,或者在代码中单独命名项目,但这会打破 DRY(不要重复自己)规则。 有人解决过这个问题吗?

Let's say I've got a folder in my project with bunch of png files in it (or mp3 files, or mov files etc).

I would like to create an NSArray populated with the items within that folder... for instance an array of UIImages for the folder of images (or just an array of NSStrings of the image name).

Is this possible? I know I could create a plist with all the names in, or name the items individually within the code, but this would break the DRY (don't repeat yourself) rule.
Has anyone worked this out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

莫言歌 2024-09-17 12:29:02

尝试 NSFileManager 的

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error

Try NSFileManager's

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error
辞别 2024-09-17 12:29:02

或者,如果您的资源位于应用程序包中,请使用 -[NSBundle pathsForResourcesOfType:inDirectory:]

 NSArray* mp3s=[[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:nil];

nil 传递给目录参数默认为顶层在 iPhone 上的应用程序包中,在 OS X 中的 Resources/ 中。

Or, if your resources are in the app bundle, use -[NSBundle pathsForResourcesOfType:inDirectory:] as in

 NSArray* mp3s=[[NSBundle mainBundle] pathsForResourcesOfType:@"mp3" inDirectory:nil];

Passing nil to the directory parameter defaults to the top level in the app bundle on iPhone, Resources/ in the OS X.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文