Reputation: 306
Current code:
@IBAction func sound1(sender: UIButton)
{
var sound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Sound1", ofType: "wav")!)
do {
try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
} catch _ {
}
do {
try AVAudioSession.sharedInstance().setActive(true)
} catch _ {
}
var error: NSError?
do {
audioPlayer = try AVAudioPlayer(contentsOfURL: sound)
} catch var error1 as NSError {
error = error1
audioPlayer = nil
}
audioPlayer.prepareToPlay()
audioPlayer.volume = 1
audioPlayer.play()
}
This is the current code I had before updating this project to Swift 2.
audioPlayer = nil
The issue I get when trying to run the app is this:
Cannot assign a value of type 'NilLiteralConvertible' to a value of type 'AVAudioPlayer'
Upvotes: 0
Views: 653
Reputation: 70098
It means that your variable audioplayer
is not an Optional and so can not be set to nil
.
Either make it an Optional then use it with try?
(note the ?
) without do catch
, or do not attempt to set it to nil
if you want to continue using try
inside do catch
.
Upvotes: 1