当我尝试使用 swift 播放音频文件时应用程序崩溃

Application crashing when I try to play audio file using swift

func playCorrectSound() {
    let soundURL: NSURL = NSBundle.mainBundle().URLForResource("correct", withExtension: "m4a")!
    correctSound = try! AVAudioPlayer(contentsOfURL: soundURL)
    correctSound.play()


}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    if correctIncorrectField.text == answerField.text && correctIncorrectField.text != "" && answerField.text != "" {
        pointsNum = pointsNum + 10
        points.text = "\(pointsNum)"
        textField.text = ""
        textField.hidden = true
        correctIncorrect.hidden = false
        correctIncorrect.textColor = UIColor.greenColor()
        correctIncorrect.text = "Correct!"
        pauseAndDoStuff()
        playCorrectSound()

    }
    if correctIncorrectField.text != answerField.text && correctIncorrectField.text != "" && answerField.text != "" {
        pointsNum = pointsNum - 5
        points.text = "\(pointsNum)"
        textField.text = ""
        textField.hidden = true
        correctIncorrect.hidden = false
        correctIncorrect.textColor = UIColor.redColor()
        correctIncorrect.text = "Incorrect!"
        pauseAndDoStuff()
    }
    textField.resignFirstResponder()
    return true
}

由于某种原因,它在

上崩溃了
let soundURL: NSURL = NSBundle.mainBundle().URLForResource("correct", withExtension: "m4a")!

行及其唯一的说法:

fatal error: unexpectedly found nil while unwrapping an Optional value.

确保 correct.m4a 文件存在于您的项目中

并且包含在Copy Bundle ResourcesTarget -> Build Phases