我刚刚将我的应用程序转换为Swift 2,当然会收到一条错误消息:
“ call 可以抛出,但未标记为“try”,并且无法处理错误。”
我确实在这里搜索了解决方法,但是答案比错误本身更使我困惑。大声笑。
在将其转换为Swift 2之前,我的应用程序运行良好。
var myPlayer = AVAudioPlayer()
var yourSound1 = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("RemSound_01", ofType: "wav")!)
func initYourSound() {
myPlayer = AVAudioPlayer(contentsOfURL: yourSound1, fileTypeHint: nil)
myPlayer.prepareToPlay()
myPlayer.volume = 1.0 // < for setting initial volume, still not perfected.
}
该错误引发以下行:
myPlayer = AVAudioPlayer(contentsOfURL:yourSound1,fileTypeHint:无)
我应该如何解决呢?我知道Swift试图“通用化”错误处理,但是通过打破最简单的代码来这样做对我来说似乎很愚蠢。
感谢您的帮助。
最佳答案
这是您要寻找的图案。
<throwing function> // compiler error in Swift 2
do { try <throwing function> } catch { }
通常,您会得到一个错误,可以进一步处理。
关于error-handling - 调用可能引发错误消息Swift 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34982477/