TypedSharedPreferences
By specifying type parameters for the keys you define, you can avoid typing mistakes when saving to SharedPreferences
.
Installation
TBD
Code examples
Define keys with type parameter.
sealed class Keys {
object Name : Key<String>
object Age : Key<Int>
object Birthday : Key<Long>
object SyncRate : Key<Float>
object HasIdCard : Key<Boolean>
object Friends : Key<Set<String>>
}
You can pass key / value pairs as Entry
.
val typedSharedPreferences = TypedSharedPreferences(preferences)
typedSharedPreferences.putString(Entry(Keys.Name, "Shinji"))
If you specify a value other than String for Keys.Name
, the compiler will tell you an error.
If you specify a key with a non-String parameter (such as Keys.Age
) as the parameter for the putString()
or getString()
, compiler will tell you an error.