简体   繁体   English

为什么 Kotlin 代码在我的项目中无法访问?

[英]Why Kotlin code unreachable in my project?

I have simple model and I want to use user profil image and name, but it is throw an error like "Unreachable code" for我有简单的 model 并且我想使用用户配置文件图像和名称,但它会抛出类似“无法访问的代码”的错误

participants.find { it.id.= userId }!!.profilePicture参与者.find { it.id.= userId }!!.profilePicture

and

participants.find { it.id.= userId }!!.username参与者.find { it.id.= userId }!!.username

class Talk(
    val participantsId: ArrayList<String>

): BaseModel(), Serializable {
    var participants = ArrayList<Data>()
    val messages = ArrayList<Message>()

    fun getProfilePicture(userId: String) {
        return
            participants.find { it.id != userId }!!.profilePicture

    }

    fun getTalkName(userId: String) {
        return
            participants.find { it.id != userId }!!.username

    }
}

any idea?任何想法?

Since in Kotlin lines don't need to end with a column ( ; ), return followed by a new line is interpreted as a return with no return value (equivalent to return; in Java).由于在 Kotlin 中的行不需要以列 ( ; ) 结尾,因此后跟新行的 return 被解释为没有返回值的返回(相当于return;在 Java 中)。

You just need to remove the new lines after the return s and change the return type of your functions with the actual types you are returning.您只需要删除return之后的新行,并使用您返回的实际类型更改函数的返回类型。 eg: (I've assumed String a return type)例如:(我假设String是一个返回类型)

fun getTalkName(userId: String): String {
    return participants.find { it.id != userId }!!.username
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM