pathForResource returns nil in Swift3.0

Due to autolayout issues, I converted a program to xCode 8 Beta and Swift 3.0. I am trying to play an audio sound bite. The fileName is in the base project directory. Bundle.main returns a valid path. I have also tried unwrapping. After the automatic conversion from the old version of Swift, the following code returns nil.


let path = Bundle.main().pathForResource(fileName, ofType: nil)

Did you try setting the ofType: parameter to the file's actual filename extension?

I have tried the ofType: parameter. I have tried the URL variations as well.

As far as I tested, the code you have shown returns non-nil value, if the actual resource designated by `fileName` really exists in the app bundle.


Does your `fileName` really contain a right resource name? Have you really added that resource to your app bundle?

Just being "in the base project directory" does not mean that file is included in your app bundle as a resource. Check again.

My Sound Bites folder is present in the "Copy Bundle Resources." I tried recompiling an old program (xCode 7.3.1) that uses sound bites extensively (and was working), but now fails with the same error as I received in xCode Beta 8. I have tried both mp3 files as well as m4a. I compiled a tutorial project that uses .caf files and it worked fine. I replaced the sound file code reference with my own .m4a filename and it failed. If I display my Sound Bites directory in xcode, the files play fine.

I converted a file to .aiff and was able to compile and play. Is Apple no longer supporting .mp3 or .m4a? I use extensive audio bites in a program (10-12MB) and if they were in another format than .m4a,the program bundle would be in the 50-60MB range.

Is Apple no longer supporting .mp3 or .m4a?

No. But that’s really besides the point. NSBundle doesn’t anything about audio file formats, it just deals with directories, files and paths/URLs. If it’s returning nil, there’s something wrong at that level, not at the audio level.

btw There are two non-obvious causes for problems with NSBundle finding things:

  • case sensitivity — iOS uses a case sensitive file system, so if you get the case wrong then the resource won’t be found.

  • hidden extensions — Sometimes a file can end up with two extensions, where the second one is hidden.

I recommend that you look at the built binary to make sure that the resource is where you expect it to be, has the right case, and has the right extension. It can be useful to use Terminal here rather than Finder.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I found one answer that worked for a very similar problem, there is probably nothing to do with code, it was the way that the file was being added to Bundle. Available at: <http://stackoverflow.com/questions/34548771/swift-how-do-i-get-the-file-path-inside-a-folder>.

I have the same problems with you, at last i solved that problem:

before you drag your file to your Project, make sure that the option "Create folder references" will be selected. NOT "Create groups"

Because if you do this, your music file will not be compiled. I hope that will help to you.

sorry for my poor english 🙂

You must make sure the file is assigned to the target. Select the file in the project navigator and in the File Inspector under Target Membership, check the checkbox

pathForResource returns nil in Swift3.0
 
 
Q