Kotlin:“何时声明"跳过条件 [英] Kotlin: "When statement" skips condition

查看:56
本文介绍了Kotlin:“何时声明"跳过条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Kotlin在android中验证表单.我的问题是,我的 when语句无法正常工作.当一个条件为真时,它不会检查其他条件.

I am trying to validate a form in android using Kotlin. My problem is, that my when statement does not work like it's supposed to work. It doesn't check for the other conditions when one is already true.

// It is possible, that both btNumber and ddSalutation are false, but it only checks for btNumber
// and then jumps out
fun isFormValid(): Boolean {
        var error = 0
            when {
                btNumber.value.isNullOrEmpty() -> {
                    btNumberEM.value = emptyFieldError
                    error = 1
                }
                btNumber.value.toString().length < 7 -> {
                    btNumberEM.value = btNumberTooFewChar
                    error = 1
                }
                ddSalutation.value == ddSalutationTextPlaceHolder -> {
                    ddSalutationEM.value = ddSalutationNotSelected
                    error = 1
                }
                etFirstName.value.isNullOrEmpty() -> {
                    etFirstNameEM.value = emptyFieldError
                    error = 1
                }
            }
            return error == 0
    }

我希望我的问题有任何道理.也许我对 when语句的理解不正确.由于以下原因,这可能无法正常工作吗?

I hope my question makes any sense. Maybe I didn't understand the when statements correctly. Does this maybe not work because of:

如果用作语句,则各个分支的值为忽略了.(就像if,每个分支可以是一个块,其值是该块中最后一个表达式的值.)〜Jetbrains

If it is used as a statement, the values of individual branches are ignored. (Just like with if, each branch can be a block, and its value is the value of the last expression in the block.) ~Jetbrains

我知道已经存在相同的问题,但是没有真正的答案可以解决这个问题.

I KNOW that there is already the same question but there was no real answer to solve this problem..

感谢您的帮助,谢谢!

推荐答案

何时正常工作

when works exactly the way it's supposed to work (emphasis mine):

何时将其参数与所有分支顺序匹配直到满足某些分支条件.

when matches its argument against all branches sequentially until some branch condition is satisfied.

因此它最多执行一个分支,第一个匹配的分支,然后停止.

So it executes at most one branch, the first matching one, and then it stops.

如果要继续匹配其他分支,只需使用一系列不相关的 if 语句代替:

If you want to continue to match other branches, just use a series of unrelated if statements instead:

// It is possible, that both btNumber and ddSalutation are false, but it only checks for btNumber
// and then jumps out
fun isFormValid(): Boolean {
    var error = 0
    if (btNumber.value.isNullOrEmpty()) {
        btNumberEM.value = emptyFieldError
        error = 1
    }
    if (btNumber.value.toString().length < 7) {
        btNumberEM.value = btNumberTooFewChar
        error = 1
    }
    if (ddSalutation.value == ddSalutationTextPlaceHolder) {
        ddSalutationEM.value = ddSalutationNotSelected
        error = 1
    }
    if (etFirstName.value.isNullOrEmpty()) {
        etFirstNameEM.value = emptyFieldError
        error = 1
    }
    return error == 0
}

这篇关于Kotlin:“何时声明"跳过条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆