【问题标题】:How to return type what I want?如何返回我想要的类型?
【发布时间】:2020-10-01 11:08:45
【问题描述】:

Android Kotlin,api.openOrder 返回类型为 BaseResponse<Profile>

我想要的代码是,如果profile.value不为null,API返回BaseResponse<Profile>,否则,抛出异常:

suspend fun openOrder(qty: Int): BaseResponse<Profile> = withContext(IO) {
    profile.value?.apply {
        return@withContext api.openOrder(username, qty, createAuthCode(username))
    } ?: throw Exception()
}

但是android studio提示这个有趣的返回类型是Any,如何解决?

排除if-else用法,因为profile.value有很多属性,而profile.value.usernameprofile.value.qty和很多profile.value.*不易阅读。

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    使用run 代替applyrun 返回 lambda 结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-22
      • 2017-03-15
      • 2021-12-24
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 1970-01-01
      相关资源
      最近更新 更多