I have this function:
func getSeconds()->Int{
return Int(Double(self.value) * (self.player.currentItem?.duration.seconds)!)
}
It sometimes crashes:
Fatal error: Double value cannot be converted to Int because it is either infinite or NaN
Of course I've seen this:
Swift 3:fatal error: Double value cannot be converted to Int because it is either infinite or NaN
But how EXACTLY do I have to use this?
What is this?:
guard !(totalSeconds.isNaN || totalSeconds.isInfinite) else {
return "illegal value"
}
Where do I return totalSeconds
if everything is fine?
EDIT:
I'm doing it like this now:
func getSeconds()->Int{
let totalSeconds = self.player.currentItem?.duration.seconds
if(totalSeconds != nil){
guard !(totalSeconds!.isNaN || totalSeconds!.isInfinite) else {
return 0 // or do some error handling
}
return Int(Double(self.value) * (self.player.currentItem?.duration.seconds)!)
}else{
return 0
}
}
Does it make sense?