Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve a list of .wav files in specific directory [duplicate]

Tags:

ios

swift

swift4

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

like image 514
WBuck Avatar asked Jan 27 '26 06:01

WBuck


1 Answers

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:

In the Finder, create a folder somewhere, named Sounds. Now 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):

enter image description here

You'll know you've got it right because the folder icon in the project navigator will be blue:

enter image description here

Confirm that you've done it right by checking that the Sounds folder appears in the Copy Bundle Resource build phase:

enter image description here

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.

like image 171
matt Avatar answered Jan 29 '26 19:01

matt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!