如何检索特定目录中的.wav文件列表 [英] How to retrieve a list of .wav files in specific directory
问题描述
我在XCode的主项目文件夹下创建了一个名为 Sounds
的目录。
然后我将8个 wav
类型的音频文件拖到 Sounds
文件夹中。
我确保在对话框提示时检查复制项目
。
我目前尝试检索位于 Sounds
目录中的所有 wav
文件的列表。
我尝试了很多种不同的方式,但下面是最新的。
Bundle。 main.paths(forResourcesOfType:wav,inDirectory:Sounds)
上面的代码,如以及我的其他尝试已经产生 nil
或空数组。
所以我的问题是,我怎么能从 Sounds
目录中检索 wav
类型的所有文件的列表?
此外,我对iOS目录结构有点不清楚。
例如, Bundle.main
中的目录列表与 DocumentDirectory
之间的差异是什么? / p>
我编辑了我的问题,因为我错误地暗示 Sounds
文件夹位于根目录中。
我在我的项目根目录中创建了一个名为Sounds的目录
这听起来像问题就在那里。你需要一个文件夹参考 - 而是你创建了一个组。
这是正确的程序:
创建一个在其他地方的文件夹 - 例如,在桌面上 - 名称声音。将该文件夹拖到项目窗口的项目导航器中,当您这样做时,请仔细查看出现的对话框。你需要它看起来像这样(注意关键的指定为文件夹引用):
你会知道你做得对,因为项目导航器中的文件夹图标将是蓝色:
通过检查是否已确认您已完成此操作Sounds文件夹出现在Copy Bundle Resource构建阶段:
现在您的策略将起作用:将文件从Finder拖到项目中的蓝色文件夹中avigator,它们将被复制到真正的Sounds文件夹中。 Sounds文件夹本身将在构建时复制到您的应用程序包中,您可以使用普通的FileManager方法在代码中引用其内容。
I created a directory underneath my main project folder in XCode named Sounds
.
I then dragged 8 audio files of type wav
into the Sounds
folder.
I ensured to check Copy items if needed
when prompted by the dialog.
I'm currently attempting to retrieve a list of all wav
files located in the Sounds
directory.
I've attempted this a number of different ways, but below is the most recent.
Bundle.main.paths(forResourcesOfType: "wav", inDirectory: "Sounds")
The above code, as well as my other attempts have either yielded nil
or an empty array.
So my question is, how can I retrieve a list of all files of type wav
from the Sounds
directory?
Furthermore, I'm a little unclear on the iOS directory structure.
For example, whats the different between the directory listings located in Bundle.main
and the DocumentDirectory
?
I have edited my question as I incorrectly implied that Sounds
folder was located in the root directory.
I created a directory in the root of my project named Sounds
That sounds like the problem right there. You need a folder reference — and instead you created a group.
Here's the correct procedure:
Create a folder somewhere else — say, on the desktop — name Sounds. Drag that folder into your project window's project navigator, and when you do, look carefully at the dialog that appears. You need it to look like this (note the crucial designation as a folder reference):
You'll know you've got it right because the folder icon in the project navigator will be blue:
Confirm that you've done it right by checking that the Sounds folder appears in the Copy Bundle Resource build phase:
Now your strategy will work: Drag files from the Finder into that blue folder in the project navigator, and they will be copied into the real "Sounds" folder. The "Sounds" folder itself will be copied into your app bundle at build time, and you can refer to its contents in code using ordinary FileManager methods.
这篇关于如何检索特定目录中的.wav文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!