Seems like the SharedPref operators I wrote last w...
# announcements
c
Seems like the SharedPref operators I wrote last week aren't working. I guess I'm misunderstanding typing. The following code will bum out
java.lang.IllegalArgumentException: This Type[java.lang.String] is not supported by SharedPreferences
regardless of the type:
Copy code
inline operator fun <reified V : Any> SharedPreferences.getValue(thisRef: Any?, property: KProperty<*>): V {
    return getValue(thisRef, property, V::class.java)
}

@Suppress("UNCHECKED_CAST")
fun <V> SharedPreferences.getValue(thisRef: Any?, property: KProperty<*>, clazz: Class<V>): V {
    val name = property.name(thisRef)
    return when (clazz) {
        String::class -> getString(name, "") as V
        Int::class -> getInt(name, 0) as V
        Float::class -> getFloat(name, 0F) as V
        Long::class -> getLong(name, 0L) as V
        Boolean::class -> getBoolean(name, false) as V
        else -> throw IllegalArgumentException("This Type[${clazz.name}] is not supported by SharedPreferences")
    }
}
Am I missing anything?