Kotlin:可空的财产代表可观察
在Kotlin中,我们可以定义一个非null属性的observable,
var name: String by Delegates.observable("<no name>") { prop, old, new -> println("$old -> $new") }
然而这是不可能的
var name: String? by Delegates.observable("<no name>") { prop, old, new -> println("$old -> $new") }
定义可空属性的可观察值的方法是什么?
编辑:这是编译错误
Property delegate must have a 'setValue(DataEntryRepositoryImpl, KProperty<*>, String?)' method. None of the following functions is suitable: public abstract operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String): Unit defined in kotlin.properties.ReadWriteProperty
- 有没有办法将后端(Kotlin)服务器应用程序部署到Firebase?
- Kotlin的Proguard混淆关键字
- Kotlin – MainActivity扩展AppCompatActivity,onRequestPermissionsResult函数未找到,不能被覆盖
- 领域,初始化一个空的字符串数组返回“不支持的类型java.lang.String ”上生成
- 用Kotlin记录
由于某种原因,类型推断在这里失败。 您必须手动指定委托的类型。 相反,您可以省略属性类型声明:
var name by Delegates.observable<String?>("<no name>") { prop, old, new -> println("$old -> $new") }