Float.isNaN 'receiver type mismatch' Kotlin 中的错误

Float.isNaN 'receiver type mismatch' error in Kotlin

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

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:

  • public inline fun Double.isNaN(): Boolean defined in kotlin public
  • inline fun Float.isNaN(): Boolean defined in kotlin

伪代码如下,感谢大家的帮助:

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

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

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

而不是

Float.isNaN(todaysResult)

你想要

todaysResult.isNaN()

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

public inline fun Double.isNaN(): Boolean defined in kotlin
public inline fun Float.isNaN(): Boolean defined in kotlin

语法 Float.isNaN() 表示此函数采用类型为 Float.

receiver

另请参阅:Extensions (Kotlin reference)