3

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.

WBuck
  • 5,162
  • 2
  • 25
  • 36
  • "I created a directory in the root of my project named Sounds." How, precisely, did you do that? Remember, a _group_ is of no use to you here; you need a _folder reference_. Do you understand the difference, and how to make a folder reference? – matt Feb 13 '18 at 02:02
  • I created the Sounds folder underneath my main project folder in XCode. I would assume is doing it this a folder reference would be automatically created. – WBuck Feb 13 '18 at 02:07
  • Well you assume wrong. What you created was a group. – matt Feb 13 '18 at 02:09
  • I assume that a group is symbolic? In other words, it's not a "real" folder (like in Visual Studio when you create a new folder for a C++ project) – WBuck Feb 13 '18 at 02:11
  • Bundle is where all the files that were included with your App are located while the documents directory is where you store the files generated while using the App – Leo Dabus Feb 13 '18 at 02:12
  • @LeoDabus So is the documents directory analogous to the temp directory in Windows? In other words it is a directory to store temporary files? – WBuck Feb 13 '18 at 02:15
  • Correct, a group is mostly just a way of organizing things in the project navigator, though it may also have an avatar in the Finder. But even in the latter case, there is no "folder" that will be copied into the bundle. That is why you need a folder reference. – matt Feb 13 '18 at 02:15
  • @WBuck No there is also a temporary folder where your app can save temporary items. All files stored at the documents directory will persist and will be backed-up with iTunes – Leo Dabus Feb 13 '18 at 02:16
  • I suggest you take some time and read about iOS File System Basics documentation from apple https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html – Leo Dabus Feb 13 '18 at 02:17

1 Answers1

5

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.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 2
    The most elegant solution. There's no need to place the referenced folder as far away as the desktop. Placing the folder at the same level as the .xcodeproj file, for instance, allows one to place the referenced folder under version control. – Elise van Looij Feb 08 '21 at 19:42
  • @ElisevanLooij That's a good point. I'll just delete the reference to the desktop; I don't think we lose any generality. – matt Feb 08 '21 at 20:02