Android Kotlin类实现的问题
当我想从sharedpreferences得到int值时,我得到了UnsupportedOperationException
但是我从logcat显示,这个类是Int
。 怎么了?
operator inline fun get(@XMLS xml: String, @KEYS key: String, defaultValue: T? = null): T { Timber.d("${T::class} + $xml + $key + $defaultValue") return when (T::class) { String::class -> getShared(xml)?.getString(key, defaultValue as? String ?: "") as? T ?: "" as T Int::class -> { Timber.d("not triggered") //< getShared(xml)?.getBoolean(key, defaultValue as? Boolean == true) as? T ?: true as T Float::class -> getShared(xml)?.getFloat(key, defaultValue as? Float ?: -1f) as? T ?: -1f as T Long::class -> getShared(xml)?.getLong(key, defaultValue as? Long ?: -1) as? T ?: -1 as T else -> throw UnsupportedOperationException("unknown class!") } }
输出:
class kotlin.Int + application_data + Ver + null
这是失败的,因为Int::class
是基本int
而T::class
是盒装typesjava.lang.Integer
。 他们两人的kotlin.Int
看起来像kotlin.Int
所以很难区分。
尽管看起来有点奇怪,
when (T::class) { Int::class, Integer::class -> }
(为了清晰起见,我把Int
留在那里,尽管它永远不会触发。)
- 我可以在Room持久性中使用两个实体的一个模型吗?
- 什么是KTX(Kotlin扩展库),为什么它在Android开发中越来越受欢迎
- 当我点击EditText时如何禁用键盘?
- 在Kotlin Realm Extention中如何解决“function不属于这个领域的模式的一部分”
- 如何显示从Android库反编译的Kotlin代码
- Kotlin-android:未解决的引用数据绑定
- AES加密。 PHP和Kotlin \ Java的区别
- 找不到引用类kotlin.internal.annotations.AvoidUninitializedObjectCopyingCheck
- Kotlin的Android扩展和variables