简体   繁体   English

分配不是表达式,在这种情况下只能使用表达式-Kotlin

[英]Assignments are not expressions and only expressions are allowed in this context - Kotlin

I am getting error while converting java to kotlin and cannot understand how to solve this particular error. 将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]
}

Following line: (diffInSeconds = diffInSeconds / 60) shows error displaying (diffInSeconds = diffInSeconds / 60)行: (diffInSeconds = diffInSeconds / 60)显示错误显示

Assignments are not expressions, and only expressions are allowed in this context 分配不是表达式,并且在此上下文中仅允许表达式

You can not do things like this: 您不能做这样的事情:

diffInSeconds = diffInSeconds / 60

in if, it is not supported by kotlin. 如果,kotlin不支持。 You have to extract it before or after if. 您必须在if之前或之后将其提取。

Eg 例如

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
    diffInSeconds /= 60
    diff[2] = if (diffInSeconds >= 60)
        diffInSeconds % 60
    else
        diffInSeconds // min
    diffInSeconds /= 60
    diff[1] = if (diffInSeconds >= 24)
        diffInSeconds % 24
    else
        diffInSeconds // hour
    diffInSeconds /= 24
    diff[0] = (diffInSeconds) // day


    return diff[0]
}

The syntax is not valid, because diffInSeconds = diffInSeconds / 60 is not an expression in Kotlin. 语法无效,因为diffInSeconds = diffInSeconds / 60在Kotlin中不是表达式。 just do this 只是这样做

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

暂无
暂无

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

相关问题 赋值不是表达式,在此上下文中只允许使用表达式 - 将Java转换为Kotlin时出错 - Assignments are not expressions, and only expressions are allowed in this context - Error when convert Java to Kotlin 分配不是表达式,在这种情况下只允许使用表达式,我知道它已经讨论过了,但是没有 - Assignments are not expressions, and only expressions are allowed in this context i know its alreaduy disccused but didn't unders Kotlin/Android 中的“赋值不是表达式”错误 - "Assignments are not expressions" error in Kotlin/Android kotlin lambda表达式作为可选参数 - kotlin lambda expressions as optional parameter 此语言级别不允许使用Android Studio Lambda表达式 - Android Studio Lambda expressions are not allowed at this language level “只能在常量表达式中使用const val”编译时错误Dagger2 Kotlin - “only const val can be used in constant expressions” compile time error Dagger2 Kotlin Kotlin 中的异步匿名函数? (拉姆达表达式) - Asynchronous anonymous function in Kotlin? (lambda expressions) 在 Kotlin lambda 表达式中覆盖多个接口方法 - Overriding multiple interface methods in Kotlin lambda expressions Kotlin:何时以及如何使用 Lambda 表达式 - Kotlin : When and How Should One Use Lambda Expressions 在kotlin中意外的令牌(使用;在同一行上分离表达式) - Unexpected tokens (use ; to seperate expressions on the same line) in kotlin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM