您可以根据文件夹内容构建项目数组吗?
假设我的项目中有一个文件夹,其中包含一堆 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试 NSFileManager 的
Try NSFileManager's
或者,如果您的资源位于应用程序包中,请使用 -[NSBundle pathsForResourcesOfType:inDirectory:] 如
将
nil
传递给目录参数默认为顶层在 iPhone 上的应用程序包中,在 OS X 中的Resources/
中。Or, if your resources are in the app bundle, use -[NSBundle pathsForResourcesOfType:inDirectory:] as in
Passing
nil
to the directory parameter defaults to the top level in the app bundle on iPhone,Resources/
in the OS X.