我試圖播放基於文件名的聲音。我用所有文件名創建了一個枚舉。一切正常,但這種情況下,我在那裏檢查了soundType.clickSwift 3枚舉函數導致應用程序崩潰
func playSound(type: soundType) {
var soundUrl = URL.init(fileURLWithPath: Bundle.main.path(forResource: type.rawValue, ofType: "aiff")!)
if type.rawValue == soundType.click.rawValue {
soundUrl = URL.init(fileURLWithPath: Bundle.main.path(forResource: type.rawValue, ofType: "wav")!)
}
do {
audioPlayer = try AVAudioPlayer(contentsOf: soundUrl)
audioPlayer.play()
} catch _ { }
}
這裏是我的枚舉
enum soundType: String {
case selectAnswer = "answerSelected"
case correctAnswer = "correctAnswer"
case wrongAnswer = "wrongAnswer"
case click = "click"
}
的問題是在這裏,我檢查「type.rawValue == soundType。 click.rawValue」
以下是錯誤
fatal error: unexpectedly found nil while unwrapping an Optional value
那麼,程序發現零,意外的是,雖然它是......解開一個'可選的'值:P這正是它在調用URL.init之前所說的 – Alexander