Search code examples
xcodebundleassets

How do I load sound resource? URLForResource returns nil


I've been following this tutorial. This line of code returns nil...

let url = NSBundle.mainBundle().URLForResource(filename as String, withExtension: "caf")

Here's my resources...

enter image description here

Here's my build phase...

enter image description here

NSBundle(forClass: self.dynamicType) doesn't improve the situation.


Solution

  • The code worked when I dragged the sound file into the project instead of into the project's asset catalogue. I'm guessing that the latter's items are referenced by id instead of file name and type.

    I also needed to add the sound file to the "Copy Bundle Resources" section in "Build Phases".