How to get path to a subfolder in main bundle?

In Swift-3 make URL, and call appendingPathComponent:

let resourcePath = Bundle.main.resourcePath
let subdir = URL(fileURLWithPath:resourcePath!).appendingPathComponent("sub").path

or simply

let subdir = Bundle.main.resourceURL!.appendingPathComponent("sub").path

(thanks, Martin R!)

See this Q&A on information on stringByAppendingPathComponent method in Swift.


You can do something like this:

let folderURL = resourceURL(to: "myfolder")

func resourceURL(to path: String) -> URL? {
    return URL(string: path, relativeTo: Bundle.main.resourceURL)
}

You can use this method to get listing of the bundle subdirectory and get resources only of the specific type:

Bundle.main.paths(forResourcesOfType: type, inDirectory: folder)