Last active
June 17, 2017 11:36
-
-
Save krupalshah/ff46326158d55511816712200f194fdc to your computer and use it in GitHub Desktop.
helper for shared prefs - kotlin version - refactoring step 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inline fun <reified T : Any> SharedPreferences.get(key: String, defaultValue: T? = null): T? { | |
return when (T::class) { | |
String::class -> getString(key, defaultValue as? String) as T? | |
Int::class -> getInt(key, defaultValue as? Int ?: -1) as T? | |
Boolean::class -> getBoolean(key, defaultValue as? Boolean ?: false) as T? | |
Float::class -> getFloat(key, defaultValue as? Float ?: -1f) as T? | |
Long::class -> getLong(key, defaultValue as? Long ?: -1) as T? | |
else -> throw UnsupportedOperationException("Not yet implemented") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment