Swift

Swift 4:fatal error: Double valu

2021-09-08  本文已影响0人  KingWorld

Swift 4:fatal error: Double value cannot be converted to Int because it is either infinite or NaN

问题

private func secondsToFormattedString(totalSeconds: Float64) -> String{
    let hours:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 86400) / 3600)

    let minutes:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 3600) / 60)
    let seconds:Int = Int(totalSeconds.truncatingRemainder(dividingBy: 60))

    if hours > 0 {
        return String(format: "%i:%02i:%02i", hours, minutes, seconds)
    } else {
        return String(format: "%02i:%02i", minutes, seconds)
    }
}

解决方法

guard !(totalsSeconds.isNaN || totalSeconds.isInfinite) else {
    return "illegal value" // or do some error handling
}
上一篇下一篇

猜你喜欢

热点阅读