Android Kotlin类实现的问题

当我想从sharedpreferences得到int值时,我得到了UnsupportedOperationException但是我从logcat显示,这个类是Int 。 怎么了?

 operator inline fun <reified T : Any> 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)?.getInt(key, defaultValue as? Int ?: -1) as? T ?: -1 as T } Boolean::class -> 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是基本intT::class是盒装类型java.lang.Integer 。 他们两人的kotlin.Int看起来像kotlin.Int所以很难区分。

尽管看起来有点奇怪,

 when (T::class) { Int::class, Integer::class -> } 

(为了清晰起见,我把Int留在那里,尽管它永远不会触发。)