【发布时间】:2023-04-02 12:57:01
【问题描述】:
在 Kotlin 中,我们可以为非空属性定义一个 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
【问题讨论】:
-
为什么你认为不可能?它应该工作相同。请说明您在使用第二个代码 sn-p 时遇到了什么问题。
-
无法编译,更新时出现编译器错误。