Skip to content

Commit

Permalink
SpUtil添加可选参数defValue
Browse files Browse the repository at this point in the history
  • Loading branch information
YeChao committed Dec 26, 2020
1 parent 9c6aaf4 commit 62be527
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/com/yechaoa/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class MainActivity : AppCompatActivity() {

button.setOnClickListener {
SpUtil.setStringSet("testStringSet", set)
//SpUtil.getBoolean("111",true)
}

button2.setOnClickListener {
Expand Down
20 changes: 10 additions & 10 deletions yutilskt/src/main/java/com/yechaoa/yutilskt/SpUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object SpUtil {
sp.edit().putString(key, value).apply()
}

fun getString(key: String?): String {
return sp.getString(key, "")
fun getString(key: String?, defValue: String = ""): String {
return sp.getString(key, defValue)
}

/**
Expand All @@ -44,8 +44,8 @@ object SpUtil {
sp.edit().putInt(key, value).apply()
}

fun getInt(key: String?): Int {
return sp.getInt(key, 0)
fun getInt(key: String?, defValue: Int = 0): Int {
return sp.getInt(key, defValue)
}

/**
Expand All @@ -55,8 +55,8 @@ object SpUtil {
sp.edit().putBoolean(key, value).apply()
}

fun getBoolean(key: String?): Boolean {
return sp.getBoolean(key, false)
fun getBoolean(key: String?, defValue: Boolean = false): Boolean {
return sp.getBoolean(key, defValue)
}

/**
Expand All @@ -66,8 +66,8 @@ object SpUtil {
sp.edit().putFloat(key, value!!).apply()
}

fun getFloat(key: String?): Float {
return sp.getFloat(key, 0f)
fun getFloat(key: String?, defValue: Float = 0f): Float {
return sp.getFloat(key, defValue)
}

/**
Expand All @@ -77,8 +77,8 @@ object SpUtil {
sp.edit().putLong(key, value!!).apply()
}

fun getLong(key: String?): Long {
return sp.getLong(key, 0)
fun getLong(key: String?, defValue: Long = 0): Long {
return sp.getLong(key, defValue)
}

/**
Expand Down

0 comments on commit 62be527

Please sign in to comment.