错误处理AVAudioPlayer的contentsOfURL

错误处理AVAudioPlayer的contentsOfURL

本文介绍了错误处理AVAudioPlayer的contentsOfURL:错误:在Swift 2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了一个教程:,我遇到了Swift 1.2和Swift 2.0之间语法发生变化的地方。

I followed a tutorial re: how to create an MP3 Player in Swift and I encountered a spot where the syntax has changed between Swift 1.2 and Swift 2.0.

我遇到了一个以下方法的错误处理问题:

I've encountered an issue with error handling for the following method:

player = AVAudioPlayer(contentsOfURL: url, error: &error)

我知道我需要使用尝试 catch 到Swift2-ify它。我已经完成了Swift 1.2代码的苹果到橙子翻译,但是我很难将它变成苹果到苹果。

I'm aware I need to use try and catch to "Swift2-ify" it. I've done an "apples to oranges" translation of the Swift 1.2 code, but I'm having difficulty making it "apples to apples".

这是相关的 Swift 1.2 中的教程中的方法/声明。

Here are the relevant methods/declarations from the tutorial in Swift 1.2.

var player: AVAudioPlayer?

func queueTrack(){
    if (player != nil) {
        player = nil
    }

    var error:NSError?
    let url = NSURL.fileURLWithPath(tracks[currentTrackIndex] as String)
    player = AVAudioPlayer(contentsOfURL: url, error: &error)

    if let hasError = error {
        //TODO: SHOW ALERT HERE
    } else {
        player?.delegate = self
        player?.prepareToPlay()
    }
}

以下是我在 Swift 2.0 中尝试的内容。它运行,但我收到警告。

Here is what I attempted in Swift 2.0. It runs, but I get warnings.

func queueTrack() {
    if (player != nil) {
        player =  nil
    }

    let url = NSURL.fileURLWithPath(tracks[currentTrackIndex] as String)
    // I get a warning to make 'var error' to 'let error' here
    // If I do what the compiler says, I get a warning the error isn't
    // initialized after 'catch' outside the curly braces
    var error: NSError? // TODO: figure out how to remove this warning


    do {
        player = try AVAudioPlayer(contentsOfURL: url)
    } catch {
        NSLog("Unresolved error \(error)")
        // SHOW ALERT OR SOMETHING
    }

    // Immutable value 'hasError' was never used; consider replacing
    // with '_' or removing it
    // If earlier declaration of error is changed to let, the warning turns
    // into an compiler error

    if let hasError = error {
        // show alert
    } else {
        player?.delegate = self
        player?.prepareToPlay()
    }
}

我在翻译中犯了什么错误?

What mistake have I made in my translation?

推荐答案

你不需要 var error:NSError? at all ,删除它和相关的行。

You don't need var error: NSError? anymore at all, delete it and the related lines.

现在你处理 catch 块中的可能错误。

Now you handle the possible error in the catch block.

func queueTrack() {

    let url = NSURL.fileURLWithPath(tracks[currentTrackIndex] as String)

    do {
        player = try AVAudioPlayer(contentsOfURL: url)
        player?.delegate = self
        player?.prepareToPlay()
    } catch {
        NSLog("Unresolved error \(error)")
        // SHOW ALERT OR SOMETHING
    }

}

请注意 catch 块中的错误变量是与以前相同的变量,它是 catch ErrorType ) >阻止。

Note that this error variable in the catch block is not the same variable as before, it's a new one (of type ErrorType) generated by the catch block.

catch 块还有另一种语法:

    do {
        player = try AVAudioPlayer(contentsOfURL: url)
        player?.delegate = self
        player?.prepareToPlay()
    } catch let error as NSError {
        NSLog("Unresolved error \(error.debugDescription)")
        // SHOW ALERT OR SOMETHING
    }

此处错误不会是 ErrorType 但像往常一样 NSError

Here the error will not be ErrorType but NSError as usual.

这篇关于错误处理AVAudioPlayer的contentsOfURL:错误:在Swift 2中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 13:18