【问题标题】:Kotlin - Set @Annotations . to function return types?Kotlin - 设置 @Annotations 。函数返回类型?
【发布时间】:2020-04-05 09:34:26
【问题描述】:

有没有办法使用注解来声明 kotlin 函数的返回条件?

例如,我想告诉 lint 我的函数将在以下位置返回一个 Android 资源 ID:

fun getImageId(): Int

我想要的东西是:

fun getImageId(): @DrawableRes Int

失败了

我认为这是有道理的(我相信在 Java 中是可能的),因为我可能有这样的东西:

 fun setImage(@DrawableRes res: Int) {
        myImageView.setImageResource(res)
    }

并将其称为:

setImage(getImageId())

因此它有一个验证链,即给定的int 实际上是一个 Res Id

【问题讨论】:

    标签: android kotlin annotations kotlin-android-extensions


    【解决方案1】:

    你可以这样做:

    @DrawableRes
    fun getImageId(): Int
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-31
      • 1970-01-01
      • 2018-08-12
      • 1970-01-01
      • 2021-04-01
      相关资源
      最近更新 更多