Kotlin

Kotlin inline和reified的用法

2018-07-09  本文已影响15人  假装在去天使之城的路上

定义:

    /**convert array response to list */
    private inline fun <reified T : XXXXXResponseInterface>
            parseListFromResponse(jsonArray: JSONArray?): List<T> {
        if (jsonArray == null) {
            return emptyList()
        }

        val listType = object : TypeToken<List<T>>() {}.type

        return Gson().fromJson(jsonArray.toString(), listType)
    }

用法:

parseListFromResponse<XXXXXResponseInterface>(jsonArray)
上一篇下一篇

猜你喜欢

热点阅读