Reputation: 2245
The Code B is from webpage, the Code A will cause the error "java.lang.String cannot be cast to java.lang.Long".
I try to analyze reason:
First the key="hello"
in SharedPreferences
store the value of String, then I hope to get a value of Long from the same key, the app crash when Code B try to convert.
I think the Code B is not good, could you fix it?
I guess the code res as T
in Code B is bad.
Code A
val key="hello"
try {
var my1: String by PreferenceTool(this, key, "-1")
my1 = "2"
val my2: Long by PreferenceTool(this, key, -1L)
var ss=my2
}catch (e:Exception){
logError(e.message?:"None")
}
Code B
class PreferenceTool<T>(private val context: Context, private val name: String, private val default: T) {
private val prefs: SharedPreferences by lazy {
context.defaultSharedPreferences
}
operator fun getValue(thisRef: Any?, property: KProperty<*>): T = findPreference(name, default)
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
putPreference(name, value)
}
@Suppress("UNCHECKED_CAST")
private fun findPreference(name: String, default: T): T = with(prefs) {
val res: Any = when (default) {
is Long -> getLong(name, default)
is String -> getString(name, default)
is Int -> getInt(name, default)
is Boolean -> getBoolean(name, default)
is Float -> getFloat(name, default)
else -> throw IllegalArgumentException("This type can be saved into Preferences")
}
res as T
}
@SuppressLint("CommitPrefEdits")
private fun putPreference(name: String, value: T) = with(prefs.edit()) {
when (value) {
is Long -> putLong(name, value)
is String -> putString(name, value)
is Int -> putInt(name, value)
is Boolean -> putBoolean(name, value)
is Float -> putFloat(name, value)
else -> throw IllegalArgumentException("This type can't be saved into Preferences")
}.apply()
}
}
Upvotes: 0
Views: 129
Reputation: 1926
SharedPreferences save types as well as key/values. For example:
<string name="last-version">8.clan.0h</string>
<int name="last-city" value="3" />
<boolean name="record-cached" value="true" />
So if you call PreferenceTool(this, key, "-1")
, you're looking for key hello
, which is saved as String
, and it will work as expected.
But if we look into the implementation for getLong in SharedPreferences class:
/**
* Retrieve a long value from the preferences.
*
* @param key The name of the preference to retrieve.
* @param defValue Value to return if this preference does not exist.
*
* @return Returns the preference value if it exists, or defValue. Throws
* ClassCastException if there is a preference with this name that is not
* a long.
*
* @throws ClassCastException
*/
long getLong(String key, long defValue);
Type for key hello
is saved as String
, so when PreferenceTool
calls getLong
you will get a ClassCastException
as documented.
You can check the documentation here:
Upvotes: 2
Reputation: 300
String in Kotlin already has an extension function you can call
toLong()
use this in your code.
Upvotes: 0