For some reason, Xcode is throwing an error when my code is run, saying
"Fatal error: unexpectedly found nil while unwrapping an Optional value".
The only problem is I am not unwrapping anything in this line of code that it says caused the error. AVAudioPlayer is a class, not a variable and so cannot be an optional.
Code:
import UIKit
import AVFoundation
class FirstViewController: UIViewController {
var label = AVAudioPlayer()
var someSounds = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("EvilLaugh", ofType: "mp3")!)
@IBAction func activation(sender: UIButton) {
label = AVAudioPlayer(contentsOfURL: someSounds, error: nil) // This is where Xcode is throwing an error
label.prepareToPlay()
label.play()
Edit: The first possible duplicate question I did check prior to asking this question which did not solve my problem, but the second I did not find prior. Please feel free to remove this question.