1

I am getting an error when I try to get a path of an audio file I have.

let path = Bundle.main.path(forResource: "SaveALife", ofType: "mp3")!

In the console I receive this:

Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

Any help? Thanks.

SeinAI0
  • 41
  • 4
  • Sounds like it's not finding it in your bundle. Have you double checked and confirmed that this is actually part of that target? Also, is the capitalization right? Devices are case sensitive. – Rob Oct 14 '16 at 04:14
  • Try `Bundle.main.url(forResource: "SaveALife", withExtension: "mp3")` – Ankita Shah Oct 14 '16 at 04:15
  • Possible duplicate of [Get resource URL from project specific path](http://stackoverflow.com/questions/33591305/get-resource-url-from-project-specific-path) – l'L'l Oct 14 '16 at 04:17

2 Answers2

1

I will go with @Rob, You must have spelled the resource name incorrectly or the file is not there in the bundle. And by providing "!" you are forcing to get string path, but as the file is not there Or due to spelling mismatch the file is not found in bundle, the return path would be nil, and due to "!" it tries to unwrap the nil which results the crash.

So the solution is remove the "!" like below

let path = Bundle.main.path(forResource: "SaveALife", ofType: "mp3")

Or else if you definitely want to use "!", You must have to give correct resource path and confirm that the resource must be there in the bundle.

Hope it helps.

Happy coding ...

Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
1

Make sure SaveALife.mp3 should be in your bundle. Also when you drag and drop the file please check copy bundle resources.

User511
  • 1,456
  • 10
  • 28