I'm new to swift and I'm learning from a course on Udemy.
I'm getting the error
"Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"
and the console explains it as "unexpectedly found nil while unwrapping an Optional value".
I've double checked to make sure my code is the exact same as my instructor's and even restarted once in case I messed something up but I'm still getting the same error.
My code is as follows :
import UIKit
import AVFoundation
class ViewController: UIViewController {
@IBOutlet weak var darkBlueBG: UIImageView!
@IBOutlet weak var powerButton: UIButton!
@IBOutlet weak var cloudHolder: UIView!
@IBOutlet weak var rocket: UIImageView!
@IBOutlet weak var hustleLbl: UILabel!
@IBOutlet weak var onLbl: UILabel!
var player: AVAudioPlayer!
override func viewDidLoad() {
super.viewDidLoad()
let path = Bundle.main.path(forResource: "hustle-on", ofType: "wav")! //This is the line that the console says is causing the error
let url = URL(fileURLWithPath: path)
do {
player = try AVAudioPlayer(contentsOf: url)
player.prepareToPlay()
} catch let error as NSError {
print(error.description)
}
}
@IBAction func powerButtonPressed(_ sender: Any) {
cloudHolder.isHidden = false
darkBlueBG.isHidden = true
powerButton.isHidden = true
player.play()
UIView.animate(withDuration: 2.3, animations: {
self.rocket.frame = CGRect(x: 0, y: 20, width: 375, height: 402)
}) { (finished) in
self.hustleLbl.isHidden = false
self.onLbl.isHidden = false
}
}
}