Error handling of AVAudioPlayer's contentsOfURL:error: in Swift 2
Error handling of AVAudioPlayer's contentsOfURL:error: in Swift 2
我遵循了一个教程:how to create an MP3 Player in Swift,我遇到了语法在 Swift 1.2 和 Swift 2.0 之间发生变化的地方。
我遇到了以下方法的错误处理问题:
player = AVAudioPlayer(contentsOfURL: url, error: &error)
我知道我需要使用 try
和 catch
来 "Swift2-ify" 它。我已经完成了 Swift 1.2 代码的 "apples to oranges" 翻译,但我很难做到 "apples to apples"。
这里是 Swift 1.2.
教程中的相关 methods/declarations
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()
}
}
我的翻译有什么错误吗?
您不再需要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
变量 不是 与之前相同的变量,它是一个新变量(类型 ErrorType
) 由 catch
块生成。
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
。
我遵循了一个教程:how to create an MP3 Player in Swift,我遇到了语法在 Swift 1.2 和 Swift 2.0 之间发生变化的地方。
我遇到了以下方法的错误处理问题:
player = AVAudioPlayer(contentsOfURL: url, error: &error)
我知道我需要使用 try
和 catch
来 "Swift2-ify" 它。我已经完成了 Swift 1.2 代码的 "apples to oranges" 翻译,但我很难做到 "apples to apples"。
这里是 Swift 1.2.
教程中的相关 methods/declarationsvar 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()
}
}
我的翻译有什么错误吗?
您不再需要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
变量 不是 与之前相同的变量,它是一个新变量(类型 ErrorType
) 由 catch
块生成。
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
。