简体   繁体   English

接收致命错误:双精度值不能转换为Int,因为它是无穷大或NaN

[英]Receiving Fatal error: Double value cannot be converted to Int because it is either infinite or NaN

The code is for a podcasting app. 该代码适用于播客应用。

import AVKit

extension CMTime {
func toDisplayString() -> String {
    let totalSeconds = Int(CMTimeGetSeconds(self))
    let seconds = totalSeconds % 60
    let minutes = totalSeconds / 60
    let timeFormatString = String(format: "%02d:%02d", minutes, seconds)
    return timeFormatString
}
}

It fails when selecting a podcast to play... resulting in audio playing but the app to freeze until rebooted. 选择要播放的播客时失败...导致播放音频,但应用程序冻结直到重新启动。

Edit: The error ocurs on line let totalSeconds = Int(CMTimeGetSeconds(self)) 编辑:错误发生在线上let totalSeconds = Int(CMTimeGetSeconds(self))

From the CMTimeGetSeconds documentation : CMTimeGetSeconds文档

If the CMTime is invalid or indefinite, NaN is returned. 如果CMTime无效或不确定,则返回NaN。 If the CMTime is infinite, +/- infinity is returned. 如果CMTime是无限的,则返回+/-无限。

When CMTimeGetSeconds returns NaN or infinity, casting the return value to an Int will throw the Fatal Error you are seeing. CMTimeGetSeconds返回NaN或无穷大时,将返回值强制转换为Int会抛出致命错误。

You can first check the value then return some sort of default in case it's not a valid number. 您可以先检查该值,然后在无效数字的情况下返回某种默认值。

func toDisplayString() -> String {
    let rawSeconds = CMTimeGetSeconds(self)
    guard !(rawSeconds.isNaN || rawSeconds.isInfinite) else {
       return "--" // or some other default string
    }
    let totalSeconds = Int(rawSeconds)
    let seconds = totalSeconds % 60
    let minutes = totalSeconds / 60
    let timeFormatString = String(format: "%02d:%02d", minutes, seconds)
    return timeFormatString
}

The below code should work... basically it happens because the value that is returned by CMTimeGetSeconds(self) is beyond Int limit. 下面的代码应该起作用...基本上会发生,因为CMTimeGetSeconds(self)返回的值超出了Int限制。

func toDisplayString() -> String {
        let totalSeconds:TimeInterval = TimeInterval(CMTimeGetSeconds(self))
        let seconds:TimeInterval = totalSeconds.truncatingRemainder(dividingBy: 60)
        let minutes:TimeInterval = totalSeconds / 60
        let timeFormatString = String(format: "%02d:%02d", minutes, seconds)
        return timeFormatString
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 3:致命错误:Double 值无法转换为 Int,因为它是无穷大或 NaN - Swift 3:fatal error: Double value cannot be converted to Int because it is either infinite or NaN 致命错误:浮点值无法转换为 Int,因为它是无穷大或 NaN - Fatal error: Float value cannot be converted to Int because it is either infinite or NaN 双精度值不能转换为Int64,因为它是无穷大或NaN - Double value cannot be converted to Int64 because it is either infinite or NaN IOS Charts 折线图视图 Double 值无法转换为 Int 因为它是无限的 - IOS Charts line chart view Double value cannot be converted to Int because it is either infinite 收到错误:“无法为索引类型为'(Any)-> Int'的类型为[[Double]'的下标” - Receiving the error: “Cannot subscript a value of type ‘[Double]’ with an index of type ‘(Any) -> Int’” 无法调用在Swift中使用int的ObjC方法,因为Int32无法转换为Int - Cannot call an ObjC method taking int in Swift because Int32 cannot be converted to Int 领域错误:无效值,期望int和接收:0 - Realm error: Invalid Value, expecting int and receiving: 0 键路径值类型“Int”无法转换为上下文类型“String” - Key path value type 'Int' cannot be converted to contextual type 'String' 致命错误:从字符串转换为 INT 时解包可选值 - fatal error: unwrapping optional value when converting from string to INT 无法将类型“ Int”的值转换为预期的参数类型“ Double” - Cannot convert value of type ‘Int’ to expected argument type ‘Double’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM