August 2017
Intermediate to advanced
440 pages
10h
English
To show more complex examples, we will present one that helps with SharedPreferences usage. There are better Kotlin approaches to this problem, but this examples is nice to analyze, and it is a reasonable example of property delegate usage on the extension property. As a result, we want to be able to treat the values saved in SharedPreferences as if they were properties of a SharedPreferences object. Here is an example usage:
preferences.canEatPie = true
if(preferences.canEatPie) {
// Code
}
Here are the examples for extension property definitions:
var SharedPreferences.canEatPie: Boolean by bindToPreferenceField(true) // 1
var SharedPreferences.allPieInTheWorld: Long by bindToPreferenceField(0,"AllPieKey") //2
Read now
Unlock full access