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

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.

1 answer

  • answered 2018-02-13 02:08 matt

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

    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.