为什么这种语法无效?IntelliJ报告的错误是在这样的上下文中只允许表达式(第2行).我想知道是否有一些语法来解决这个问题,因为Java允许在循环功能中使用这种类型的赋值.
var c: Int;
while ((c = reader.read()) != 1) {
}
Run Code Online (Sandbox Code Playgroud) 我有一个android EditText,我正在设置文本属性.
通常我会用:
editText.text = "Mars"
Run Code Online (Sandbox Code Playgroud)
但是setter返回一个Editable,所以看起来Kotlin试图用失败的String替换返回的Editable.
所以"解决方法"是:
editText.setText("Mars")
Run Code Online (Sandbox Code Playgroud)
setText()当使用这种类型的setter时,有没有更漂亮的方法(而不是)设置文本?
在Java中,有时我会编写如下代码:
String obj = null;
while ((obj = getObject()) != null) {
// do smth with obj
}
Run Code Online (Sandbox Code Playgroud)
在Kotlin中显示编译时错误:
赋值不是表达式,在此上下文中只允许使用表达式
什么是Kotlin最好的等价物?