xcode - Swift Error 未处理且未标记为 try

标签 xcode swift

自从我更新 Xcode 以来,我目前遇到了那段代码的错误

{!}Call can throw, but is not marked with 'try' and the error is not handled

   let soundURL = NSBundle.mainBundle().URLForResource("jump", withExtension: "caf")
            audioPlayer = AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
            audioPlayer.numberOfLoops = -1
            audioPlayer.play()

最佳答案

该方法现在是可抛出的,所以您可以这样处理它:

do {
    audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
    audioPlayer.numberOfLoops = -1
    audioPlayer.play()
} catch {

}

关于xcode - Swift Error 未处理且未标记为 try,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33318113/

相关文章:

ios - 在加载 tableView 时展开可选值时意外发现 nil

swift - 为什么我可以在没有框架引用的情况下使用 MessageUI?

objective-c - 将未知行数添加到 'Static Cells' UITableView

ios - 忽略库 .xcodeproj 文件更改

ios - 忽略 com.apple.coremedia.networkbuffering 中的异常

arrays - Swift 中的字典数组

ios - 在 deleteItems 后停止 UICollectionView 更新布局

iphone - 如何在 objective-c 中找到数组索引值

ios - 如果 app delegate 想快速使用主 Storyboard文件,它必须实现 window 属性

swift - 如何从 Swift 3 中的不透明指针创建类型化指针?