【问题标题】:Error handling of AVAudioPlayer's contentsOfURL:error: in Swift 2Swift 2 中 AVAudioPlayer 的 contentOfURL:error: 错误处理
【发布时间】:2015-10-20 14:14:51
【问题描述】:

我遵循了一个教程:how to create an MP3 Player in Swift,我遇到了一个在 Swift 1.2 和 Swift 2.0 之间语法发生变化的地方。

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

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

我知道我需要使用 trycatch 来“Swift2-ify”它。我已经完成了 Swift 1.2 代码的“从苹果到橘子”的翻译,但是我很难将它变成“从苹果到苹果”。

以下是 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 中尝试的。它运行,但我收到警告。

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()
    }
}

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

【问题讨论】:

    标签: ios swift swift2 avaudioplayer


    【解决方案1】:

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

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

    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 块中的这个error 变量不是与以前相同的变量,它是由catch 块生成的新变量(ErrorType 类型)。

    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
        }
    

    这里的error 不会像往常一样是ErrorType 而是NSError

    【讨论】:

    • 太棒了!感谢您让我如此迅速而简洁地解决问题。非常感谢您在这方面的帮助。
    猜你喜欢
    • 1970-01-01
    • 2016-08-02
    • 2015-09-06
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    相关资源
    最近更新 更多