How to get duration from AVMIDIPlayer

AVMIDIPlayer has an instance property "duration":


var duration: TimeInterval { get }


So I've written:


let time = midiPlayer?.duration as! TimeInterval
print (time)


which runs, but of course I get error messages about 'forced cast from 'TimeInterval?' to 'TimeInterval' (aka 'Double') only unwraps optionals; did you mean to use '!'?


What am I doing wrong? (It's usually not enough or too many question marks....😁 )

There's no point in casting, if you want to force unwrap then you just do this:


let time = midiPlayer!.duration


But it will crash if your `midiPlayer` is nil.

How to get duration from AVMIDIPlayer
 
 
Q