简体   繁体   English

由于实例是可变属性,因此无法进行智能投射

[英]smart cast impossible because instance is mutable property

I'm trying to make a singleton in Kotlin and am running into problems because I get a smart cast to PresenterManager is impossible because instance is mutable property that could have been changed at this time . 我试图在Kotlin中创建一个单例,并且遇到了问题,因为无法smart cast to PresenterManager is impossible because instance is mutable property that could have been changed at this time

This seems like a pretty standard way to make a singleton. 这似乎是制作单例的一种非常标准的方法。 Why won't it let me and how can I fix it? 为什么它不让我使用,我该如何解决?

PresenterManager {
    //some code
    ....

    companion object {
        private val PRESENTER_ID = "presenter_id"
        private var instance: PresenterManager? = null

        fun getManager(): PresenterManager {
            if (instance == null) {
                instance = PresenterManager(10, 30, TimeUnit.SECONDS)
            }
            return instance
        }
    }
}

This seems like a pretty standard way to make a singleton. 这似乎是制作单例的一种非常标准的方法。

I do recommend you to read a bit more about Kotlin. 我建议您多读一些有关Kotlin的文章。

object PresenterManager {
    init {
       // init code
    }

    fun whatever() {}
}

What I wrote above is a Singleton in Kotlin. 我上面写的是科特林的一个单例。 Now, to explain the message you are getting: 现在,解释一下您得到的消息:

smart cast to PresenterManager is impossible because instance is mutable property that could have been changed at this time 无法将智能类型转换为PresenterManager,因为实例是可变属性,该属性可能在此时已被更改

instance is nullable ( private var instance: PresenterManager? = null ), and the getManager function expects a non-null return type, so one of the many ways of solving this is by either make getManager return a nullable type ( fun getManager(): PresenterManager? ) or make use of the !! instance是可为null的( private var instance: PresenterManager? = null ),并且getManager函数期望返回非null的返回类型,因此解决此问题的多种方法之一是使getManager返回可为null的类型( fun getManager(): PresenterManager? )或使用!! operator on your return type. 返回类型上的运算符。

The main point is that you really don't need of that instance variable at all if you use an object instead of a class to declare your singleton. 要点是,如果您使用object而不是class来声明您的单例,那么您实际上根本不需要该instance变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 智能转换为“CLASS”是不可能的,因为“VARIABLE”是一个可变属性,此时可能已更改 - Smart cast to ‘CLASS!’ is impossible, because ‘VARIABLE’ is a mutable property that could have been changed by this time 智能转换为“USER”是不可能的,因为“whoIfollow”是一个可变属性,此时可能已经更改 - Smart cast to 'USER' is impossible, because 'whoIfollow' is a mutable property that could have been changed by this time 智能转换为“RecyclerView”是不可能的,因为“recentRecycler”是一个可变属性,此时可能已更改 - Smart cast to 'RecyclerView!' is impossible, because 'recentRecycler' is a mutable property that could have been changed by this time 智能投射到“ObserverT.!” 是不可能的,因为 'item.get()' 是一个复杂的表达式 - Smart cast to 'ObserverT!!' is impossible because 'item.get()' is a complex expression Kotlin 2 错误:错误:意外标记(使用 ';' 分隔同一行上的表达式)和错误:智能转换为 'Int' 是不可能的,因为 - Kotlin 2 Errors: error: unexpected tokens (use ';' to separate expressions on the same line) AND error: smart cast to 'Int' is impossible, because Smartcast 是不可能的,因为属性有开放或自定义的 getter - Smartcast is impossible because property has open or custom getter SetText不可能,因为线程错误 - SetText Impossible because bad thread 可变实例对象的可见性 - Visibility of mutable instance objects Java,不可能将对象强制转换为浮动.....为什么? - Java, impossible cast Object to Float.....why? 使用实例的类型转换 - Cast type using an instance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM