gpt4 book ai didi

kotlin - 我怎样才能在 Kotlin 中什么都不做?

转载 作者:行者123 更新时间:2023-12-02 12:17:16 24 4
gpt4 key购买 nike

ESelect 是一个enum 结构。我希望在下面的代码中它是 ESelect.NoAction 时会注意到。

如果在ESelect.NoAction ->之后写;会导致编译错误,如何解决?

代码

aHomeViewModel.selectAction.observe(mLifecycleOwner, {
when(it) {
ESelect.SelectAll - > binding.chSelect.isChecked = true
ESelect.UnselectAll - > binding.chSelect.isChecked = false
ESelect.NoAction - > ; //It will do nothing
}
})

enum class ESelect {
SelectAll,
UnselectAll,
NoAction
}

最佳答案

您可以返回 Unit(类似于 Java 中的 void)。代码将如下所示:

aHomeViewModel.selectAction.observe(mLifecycleOwner, {
when(it) {
ESelect.SelectAll -> binding.chSelect.isChecked = true
ESelect.UnselectAll -> binding.chSelect.isChecked = false
ESelect.NoAction -> Unit
}
})

查看文档:https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/

关于kotlin - 我怎样才能在 Kotlin 中什么都不做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64382462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com