错误处理AVAudioPlayer的contentsOfURL:错误:在Swift 2中

编程入门 行业动态 更新时间:2024-10-28 04:25:57
本文介绍了错误处理AVAudioPlayer的contentsOfURL:错误:在Swift 2中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遵循了一个教程:如何在Swift中创建一个MP3播放器,我遇到了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 阻止。

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中

本文发布于:2023-11-26 16:05:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   AVAudioPlayer   Swift   contentsOfURL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!