【问题标题】:Swift Error is not handled and not marked with trySwift 错误未处理且未标有 try
【发布时间】:2015-10-24 12:20:06
【问题描述】:

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

{!}调用可以抛出,但没有标记'try'并且错误不是 处理过

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

【问题讨论】:

标签: xcode swift


【解决方案1】:

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

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

}

【讨论】:

    【解决方案2】:

    使用 Swift 2,您应该处理异常。下面的代码应该可以解决您的问题:

    do {
        audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
    } catch (_) {
    
    }
    

    【讨论】:

    • 请发表反对投票的理由。我没有发布完整的代码,但肯定会在这里指出错误的原因。
    • 这是一个正确答案,因为它修复了您遇到的错误。
    • 确实,这是一个很好的答案,因为它展示了如何在不提供完整的插入式代码的情况下解决问题。 (已投票)。
    猜你喜欢
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    • 2016-01-24
    相关资源
    最近更新 更多