0

I'm making a calc just for fun and I want to add a sound for when you press a button.

The error is:

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

On this line of code:

let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "219069_annabloom_click1", ofType: "wav")!)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • let alertsound= that is where the error starts – tech stuff Oct 11 '17 at 06:06
  • Print `Bundle.main.urls(forResourcesWithExtension: "wav", subdirectory: nil)` to check if the resource is available in the bundle. If the sound is in a subdirectory, pass its name. – vadian Oct 11 '17 at 06:51
  • Force unwrapping is your enemy: remove the exclamation marks in your code and guard them to check the value is available, it will prevent your app from crashing - worst case you won't hear the sound but you can easily debug it checking if it guarded correctly. – Marco Pace Oct 11 '17 at 08:31

1 Answers1

0

I think the problem at here

 let alertSound = URL(fileURLWithPath: Bundle.main.path(forResource: "219069_annabloom_click1", ofType: "wav")!)

meaning of this sign is ! the value is not nil. But I think you have't added the file correctly or there is issue with .wav extension double check it so it returns nil and app is crashed.

Step 1: make sure you have added sound file correctly

Step 2: verify extension is same in code and file

make sure the module is selected

enter image description here

Jaydeep Vyas
  • 4,411
  • 1
  • 18
  • 44