簡體   English   中英

賦值不是表達式,在這種情況下只允許使用表達式 - Kotlin

[英]Assignments are not expressions, and only expressions are allowed in this context - Kotlin

bindingSub.btnCoinup.setOnClickListener {
    fbFirestore?.collection("users")?.document(fbAuth?.uid.toString())?.get()?.addOnSuccessListener { document ->
        if (document != null) {
            var autoCoin = document.data?.values.toString().replace("[", "").replace("]", "").toLong()
            bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}")
        } else {
            bindingSub.tvAutoCoin.setText("Coin: 0")
        }
    } ?.addOnFailureListener { exception ->
        ...

bindingSub.tvAutoCoin.setText("硬幣:${autoCoin += 1}")

我非常努力地修復了這個錯誤,但我無法修復它。

即使代碼很臟,如果您理解,我將不勝感激...

您在這一行中為autoCoin變量分配了一個新值:

bindingSub.tvAutoCoin.setText("Coin: ${autoCoin += 1}")

字符串模板只允許表達式。 表達式是產生某種類型值的東西,即使該類型是 Unit 並且可以位於賦值語句的右側(例如val x = 3將表達式3的值賦給x )。 某些語言(如 Java 或 C++)賦值是表達式,但在 Kotlin 中它們不是。

x += 1運算符是x = x + 1的特殊速記語法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM