1
votes
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
}

For some reason its crashing on the

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

line and its only saying:

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

1

1 Answers

3
votes

Make sure that correct.m4a file is exist in your project

and It is included in Copy Bundle Resources in Target -> Build Phases

enter image description here