首页 > 解决方案 > 赋值不是表达式,在这种情况下只允许使用表达式 - Kotlin

问题描述

我在将 java 转换为 kotlin 时遇到错误,无法理解如何解决这个特定错误。

internal fun getDiff(to: Calendar, from: Calendar): Long {

        var diffInSeconds = (to.time.time - from.time.time) / 1000

        val diff = longArrayOf(0, 0, 0, 0)
        diff[3] = if (diffInSeconds >= 60) diffInSeconds % 60 
                    else diffInSeconds // sec
        diff[2] = if ((diffInSeconds = diffInSeconds / 60)>= 60)
                         diffInSeconds % 60
                 else
                        diffInSeconds // min
        diff[1] = if ((diffInSeconds = diffInSeconds / 60) >= 24)
                        diffInSeconds % 24
                 else
                        diffInSeconds // hour
        diff[0] = (diffInSeconds = diffInSeconds / 24) // day

        Log.e("days", diff[0].toString() + "")

        return diff[0]
}

以下行:(diffInSeconds = diffInSeconds / 60)显示错误显示

赋值不是表达式,在此上下文中只允许使用表达式

标签: androidandroid-studiokotlin

解决方案


语法无效,因为diffInSeconds = diffInSeconds / 60它不是 Kotlin 中的表达式。就这样做

var a = diffInSeconds /= 60
diff[1] = if (a >= 24)

推荐阅读