gpt4 book ai didi

kotlin - 注释类未将输入验证为Kotlin中的枚举

转载 作者:行者123 更新时间:2023-12-02 13:00:19 26 4
gpt4 key购买 nike

我想创建注释,而不是枚举,当语句时将其用于

@Retention(AnnotationRetention.SOURCE)
@IntDef(SELECT.toLong(), WEAR.toLong(), BAND.toLong())
annotation class CurrentState

companion object {
const val SELECT = 0
const val WEAR = 1
const val BAND = 2
}

private fun handleFragment(@CurrentState state:Int) {
val fragment:Fragment =
when(state){
SELECT -> SelectDeviceFragment.newInstance()
WEAR -> ConnectWatchFragment.newInstance()
BAND -> SelectDeviceFragment.newInstance()
}

据我了解,此代码应阻止我执行以下代码:
handleFragment(5)

和when语句不应要求我添加语句,如枚举预期
我在做错还是误解?

最佳答案

https://discuss.kotlinlang.org/t/intdef-and-stringdef-not-being-checked-at-compile-time/7029:

This checking doesn’t come from the compiler, but from Android lint. Work to make android lint language independent is being done, but if I’m not mistaken you’ll need a newer version of Android Studio for it.



Android Studio 3.1 blog post提到了针对Kotlin的lint检查功能(尽管并没有说明是否特别支持该检查)。

关于kotlin - 注释类未将输入验证为Kotlin中的枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50345057/

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