xcode Swift无法在合理时间内对表达式进行类型检查[重复]

fkaflof6  于 2023-01-27  发布在  Swift
关注(0)|答案(1)|浏览(150)
    • 此问题在此处已有答案**:

The compiler is unable to type-check this expression swift 4?(15个答案)
昨天关门了。
以下函数触发错误:* "编译器无法在合理的时间内对此表达式进行类型检查;尝试将表达式分解为不同的子表达式"*

func calculateCalSpent(age: Int, height: Int, currentWeight: Int, activityLevel: Int ) -> Int{
    var cal = Int(66.47 + (13.75 * currentWeight) + (5.003 * height) - (6.755 * age))
    print(cal)
    return cal
    // Men kg cm
}

当我用1代替cal作为回报时,一切都没问题,我该怎么办呢?

zvms9eto

zvms9eto1#

你混合了整数和浮点值。在这种特殊情况下,这似乎给类型检查器带来了问题。
由于这里需要浮点计算,请将所有Int显式转换为Double以修复/解决此问题:

var cal = Int(66.47 + (13.75 * Double(currentWeight)) + (5.003 * Double(height)) - (6.755 * Double(age)))

相关问题