Get resource URL from project specific path

mattgabor picture mattgabor · Nov 8, 2015 · Viewed 27.7k times · Source

I need to retrieve the URL of a resource contained in my Xcode project through a path that begins in the project's directory (aka mainBundle)

So if my specified path if ./snd/archer/acknowledge1.wav, I need to create a URL from that.

I know I can use NSURL(fileURLWithPath:) for system directories, but I don't know how to do this directly from the bundle.

Answer

Guig picture Guig · Aug 12, 2016

As of Swift 3, the answer is:

Bundle.main.url(forResource: "acknowledge1", withExtension: "wav", subdirectory: "snd/archer")