MysteryPancake
MysteryPancake

Reputation: 1505

Swift: Get subpaths in path and files in subpaths?

My app has this sort of file structure for the sounds in it:

enter image description here

A "Sound" folder with a bunch of numbered subfolders, such as "1", "2", "3" and so on.

The problem is, I have no clue how to find the folder "1", let alone the files in it. I tried this:

for path in Bundle.main.paths(forResourcesOfType: "wav", inDirectory: "Sound") {
  print(path)
}

At no point does this print "Test.wav", or "1".

Any help would be fantastic!

EDIT: If I set the path to "", then it prints the folder "1". However, it also prints all the wav files. What is the type for a folder supposed to be?

Upvotes: 1

Views: 900

Answers (1)

rmaddy
rmaddy

Reputation: 318944

I would get the path to the Sound folder and then use FileManager to get the contents of the Sound folder.

if let soundURL = Bundle.main.path(forResource: "Sound", withExtension: nil) {
    let urls = FileManager.default.contentsOfDirectory(at: soundURL, includingPropertiesForKeys: nil, options: [ .skipsHiddenFiles ]) {
        // iterate the list of URLs as needed
    }
}

I leave it as an exercise to deal with exception handling.

Upvotes: 2

Related Questions