gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:05:20 60 4
gpt4 key购买 nike

当我调用此方法时,收到错误。如何在分配小时、分钟或秒时处理 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)
}
}

最佳答案

您应该检查 totalSeconds 是否为有效值,例如:

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

并检查一下: Convert Float to Int in Swift

关于 swift 3 :fatal error: Double value cannot be converted to Int because it is either infinite or NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52795823/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com