this function works perfectly well in swift 1.1
class func wav(filename:NSString) -> AVAudioPlayer {
let url = NSBundle.mainBundle().URLForResource(filename, withExtension: "wav")
var error:NSError? = nil
let player = AVAudioPlayer(contentsOfURL: url, error: &error)
if error != nil {
println("Error loading \(url): \(error?.localizedDescription)")
} else {
player.prepareToPlay()
}
player.volume = 0.2
return player
}now i'm converting it to swift v2, ios 9, xcode 7
finally i got it to work with the following, throwing out the error with try!
class func wav(filename:NSString) -> AVAudioPlayer {
let url = NSBundle.mainBundle().URLForResource(filename as String, withExtension: "wav")
let player = try! AVAudioPlayer(contentsOfURL: url!)
player.prepareToPlay()
player.volume = 0.2
return player
}how would you rewrite this to use the do, try, catch???