ESelectenum结构。我希望它在下面的代码中为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/

10-10 03:38