ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Kotlin(六) 高阶函数的应用

2020-11-22 11:32:11  阅读:226  来源: 互联网

标签:函数 SharedPreferences Kotlin value contentValues key put ContentValues 高阶


Kotlin(六) 高阶函数的应用

高阶函数非常适用于简化各种API的调用,一些API原有用法在使用高阶函数进行简化后其可读性和易用性方面会有很大的提高。

1、简化SharedPreferences的使用

在简化前先来回顾下SharedPreferences的使用:

  • 1、通过Context.getSharedPreferences()或Activity.getPreferences()获取SharedPreferences对象。
  • 2、通过SharedPreferences.edit()获取SharedPreferences.Editor对象
  • 3、调用Editor的一系列putXX()方法去保存数据
  • 4、最后调用Editor的apply()方法进行保存数据的提交
    代码如下:
val sp = getPreferences(Context.MODE_PRIVATE)
val editor = sp.edit()
editor.apply {
    putString("name", name)
    putInt("age", age)
    apply()
}

虽然API的使用虽然已经很简单但是我们依然可以使用高阶函数对其进行简化,首先创建一个SharedPreferences.kt文件,然后在文件中定义save()方法

inline fun SharedPreferences.save(block: SharedPreferences.Editor.() -> Unit) {
    val editor = edit()
    editor.block()
    editor.apply()
}

下面解释一下这个高阶函数:

  • 1、首先通过SharedPreferences.save()定义了SharedPreferences的一个扩展函数,这样我们就能通过SharedPreferences对象调用save()了,并且save()就拥有了SharedPreferences的上下文。
  • 2、save()函数接收一个函数型的参数,SharedPreferences.Editor.()表示这个函数类型参数是定义在SharedPreferences.Editor中的,这个函数型的具体实现中就拥有了SharedPreferences.Editor的上下文,并且需使用SharedPreferences.Editor对象去调用block()
  • 3、别忘记了将高阶函数声明为内联函数,减少额外的内存损耗
    定义好了高阶函数save(),下面看下具体的使用
 getSharedPreferences("LoginActivity", Context.MODE_PRIVATE).save {
            putString("name", "阿三")
            putInt("age",19)
        }

这里我们没有调用apply()或commit(),高阶函数内部已经调用了,是不是很简单啊。
最后不得不提一下Google中提供了KTX扩展库中包含了SharedPreferences的简化用法,我们在module的gradle中导入implementation 'androidx.core:core-ktx:1.3.1’即可使用KTX扩展库了。下面看下KTX扩展库中对SharedPreferences的简化使用

getSharedPreferences(“LoginActivity”, Context.MODE_PRIVATE).edit {
putString(“name”, “阿三”)
putInt(“age”, 19)
}
感兴趣的可以看下edit的定义,和我们刚才定义的高阶函数基本是一样的。

2、简化ContentValue的使用

ContentValues的基本用法如下:

val contentValues = ContentValues()
contentValues. put("autor", "曹雪芹")
contentValues. put("price", 400)
contentValues.  put("pages", 4000)   

在真正开始简化之前,我们先来看下mapOf()用法。

val map= mapOf(1 to "Apple",
               2 to "Banana",
               3 to "Orange")

可以看到使用1 to "Apple"创建键值对,在Kotlin中使用A to B语法结构会创建一个Pair对象,有了这个知识后我们创建一个ContentValues.kt文件在文件中创建方法build()方法,用于生成ContentValues对象。

fun build(vararg pairs: Pair<String, Any?>): ContentValues {}
  • 首先build()函数接收一个Pair类型的参数,也就是使用A to B生成Pair对象。
  • 参数前加了vararg关键字,表示build()接收可变参数,类似于Java的public void test(String… strArray)
  • 然后我们就能通过遍历pairs来向ContentValues中添加数据,但是有个问题就是ContentValues中的value中不支持所有类型,所以只能通过when类型判断进行添加数据,并且还是用了Kotlin的SmartCast功能,比如when语句进入Int类型后,value就自动被转换成了Int不再是Any类型。
fun build(vararg pair: Pair<String, Any?>): ContentValues {
    val contentValues = ContentValues()
    for (item in pair) {
        val key = item.first
        val value = item.second
        when (value) {
            is String -> contentValues.put(key, value)
            is Long -> contentValues.put(key, value)
            is Int -> contentValues.put(key, value)
            is Double -> contentValues.put(key, value)
            is ByteArray -> contentValues.put(key, value)
            is Boolean -> contentValues.put(key, value)
            is Float -> contentValues.put(key, value)
            is Short -> contentValues.put(key, value)
            is Byte -> contentValues.put(key, value)
            null -> contentValues.putNull(key)
        }
    }
    return contentValues
}

有了build方法后,使用ContentValues就变得很简单了

val contentValues = build(
            "autor" to "曹雪芹",
            "price" to 400,
            "pages" to 4000
        )

这样我们就能通过类似mapOf()方式来构建ContentValues了,但是这和高阶函数没有任何关系,下面我们结合apply{}来简化一下:

fun build(vararg pair: Pair<String, Any?>)=ContentValues().apply {
    for (item in pair) {
        val key = item.first
        val value = item.second
        when (value) {
            is String -> put(key, value)
            is Long -> put(key, value)
            is Int -> put(key, value)
            is Double -> put(key, value)
            is ByteArray -> put(key, value)
            is Boolean -> put(key, value)
            is Float -> put(key, value)
            is Short -> put(key, value)
            is Byte -> put(key, value)
            null -> putNull(key)
        }
    }
}

如果你很熟悉apply{}的话,代码也很好理解,我们使用ContentValues()对象调用apply,ContentValues()对象传入Lambda表达式中并作为其上下文,而且apply的返回值就是调用者本身。其实KTX扩展库中也提供了类似方法contentValuesOf来实现这个功能,感兴趣的可以自行实现。

标签:函数,SharedPreferences,Kotlin,value,contentValues,key,put,ContentValues,高阶
来源: https://blog.csdn.net/weixin_40152040/article/details/109923183

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有