Fabian Boulegue
Fabian Boulegue

Reputation: 6608

Swift Error is not handled and not marked with try

I currently run into a error with that bit of code since i updated Xcode

{!}Call can throw, but is not marked with 'try' and the error is not handled

   let soundURL = NSBundle.mainBundle().URLForResource("jump", withExtension: "caf")
            audioPlayer = AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
            audioPlayer.numberOfLoops = -1
            audioPlayer.play()

Upvotes: 0

Views: 129

Answers (2)

Abhinav
Abhinav

Reputation: 38162

With Swift 2, you should be handling exceptions. Below code should fix your problem:

do {
    audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
} catch (_) {

}

Upvotes: 1

joern
joern

Reputation: 27620

The method is now throwable so you have handle it this way:

do {
    audioPlayer = try AVAudioPlayer(contentsOfURL: soundURL!, fileTypeHint: nil)
    audioPlayer.numberOfLoops = -1
    audioPlayer.play()
} catch {

}

Upvotes: 2

Related Questions