Search code examples
error-handlingswift2do-catch

Method that returns Void which also throws causes annoying warning in editor


I use this method:

public func setCategory(category: String, withOptions options: AVAudioSessionCategoryOptions) throws

It's an AVAudioSession method and as you can see it doesnt return anything but it should throw and error.

I do like this:

try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)

This gives me an annoying warning:

Result of 'try?' is unused

I tried to set it into a variable and put it into a do-catch but still the same warning...

How can I get rid of this warning?


Solution

  • Just replace ? with ! and it will look like:

    try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)
    

    And your warning will gone.

    UPDATE:

    This won't crash:

    _ = try? AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: .MixWithOthers)