Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get resource URL from project specific path

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.

like image 909
mattgabor Avatar asked Nov 08 '15 06:11

mattgabor


2 Answers

As of Swift 3, the answer is:

Bundle.main.url(forResource: "acknowledge1", withExtension: "wav", subdirectory: "snd/archer")
like image 65
Guig Avatar answered Oct 18 '22 21:10

Guig


You would use one of the bundle resourse URL methods

[[NSBundle mainBundle] URLForResource:@"acknowledge1"
                        withExtension:@"wav"
                         subdirectory:@"snd/archer"];

NSBundle.mainBundle().URLForResource("acknowledge1", withExtension:"wav" subdirectory:"snd/archer")

In latest Swift:

Bundle.main.url(forResource: "acknowledge1", withExtension:"wav")
like image 37
Wain Avatar answered Oct 18 '22 20:10

Wain