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.
最佳答案
关于ios - 当我尝试使用 swift 播放音频文件时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005147/