lateinitVar无法解析为使用Kotlin 1.2.10中的isInitialized [英] lateinitVar cannot be resolved to use isInitialized from Kotlin 1.2.10
问题描述
我想使用此功能
示例中最简单的方法对我不起作用:
the simplest thing like in the example does not work for me:
lateinit val foo = 1
val bar = foo::lateinitVar.isInitialized()
但是我得到
unresolved reference lateinitVar
我正在通过Android Studio中的gradle使用Kotlin 1.2.10(也在那里安装了Kotlin 1.2.10插件)
I am using Kotlin 1.2.10 via gradle in Android-Studio (also the Kotlin 1.2.10 plugin installed there)
推荐答案
您的代码中没有名称为lateinitVar
的变量,因此出现错误.再次查看示例:
There’s no variable with name lateinitVar
in your code, thus the error. Look at the example again:
this::lateinitVar.isInitialized
在this
中定义了一个变量lateinitVar
,该函数被调用.可以扩展示例中的代码片段(列表开头的小加号),其外观如下:
There’s a variable lateinitVar
defined in this
, which the function is called on. The code snippet in the example can be expanded (little plus sign at the beginning of the listing) and looks as follows:
class Foo {
lateinit var lateinitVar: String
fun initializationLogic() {
println("isInitialized before assignment: " + this::lateinitVar.isInitialized)
lateinitVar = "value"
println("isInitialized after assignment: " + this::lateinitVar.isInitialized)
}
}
这可能使它更清楚.
此外,请注意,lateinit
不能应用于val
,而只能应用于var
.
Also, be aware that lateinit
can’t be applied to val
but only var
.
这篇关于lateinitVar无法解析为使用Kotlin 1.2.10中的isInitialized的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!