Swift - How do I get the file path inside a folder

First make sure when you drag your folder audioFiles to your project to check copy items if needed and select create folder references. Make sure it shows a blue folder if your project.

enter image description here

enter image description here

Also NSBundle method pathForResource has an initialiser that you can specify in which directory your files are located:

let audioFileName = "audioName" 

if let audioFilePath = Bundle.main.path(forResource: audioFileName, ofType: "mp3", inDirectory: "audioFiles") {
    print(audioFilePath)
}

If you would like to get that file URL you can use NSBundle method URLForResource(withExtension:, subdirectory:)

if let audioFileURL = Bundle.main.url(forResource: audioFileName, withExtension: "mp3", subdirectory: "audioFiles") {
    print(audioFileURL)
}

Tags:

Ios

Swift