【问题标题】:Float.isNaN 'receiver type mismatch' error in KotlinKotlin 中的 Float.isNaN 'receiver type mismatch' 错误
【发布时间】:2020-12-07 12:04:36
【问题描述】:

我是 Kotlin 的新手,对 Kotli'ns 的内置 FLoat.isNaN 和 Double.isNaN 函数有疑问。当使用 Float.isNaN 函数测试浮点数组列表的 NaN 等于时,我收到错误:

未解决的参考。由于接收器类型不匹配,以下候选均不适用:

  • public inline fun Double.isNaN():在 kotlin public 中定义的布尔值
  • inline fun Float.isNaN():在 kotlin 中定义的布尔值

下面列出了伪代码,感谢任何帮助:

var scores = arrayListOf<Float>()
val todaysResult = scores[0]
if(Float.isNaN(todaysResult)) {
    todayResultNumericTextView!!.text = "-"
} else {
    todayResultNumericTextView!!.text = Math.round(todaysResult).toString() + "%"
}

【问题讨论】:

  • 你需要做todaysResult.isNan()

标签: kotlin types casting double nan


【解决方案1】:

isNaNFloatDouble 的扩展函数(不是“静态”方法,不像Java!)这意味着您必须使用值作为接收者来调用它。

fun Double.isNaN(): Boolean
fun Float.isNaN(): Boolean

代替

Float.isNaN(todaysResult)

你想要的

todaysResult.isNaN()

错误信息也表明了这一点:

public inline fun Double.isNaN(): Booleankotlin 中定义
public inline fun Float.isNaN(): Booleankotlin 中定义

语法Float.isNaN() 表示此函数采用Float 类型的接收器


另见:Extensions (Kotlin reference)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多