0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

kotlinのエルビス演算子

Posted at

kotlinのエルビス演算子

書き方

- val c = a :? ""

  • もしaはnullじゃないならc = a
  • もしaはnullならc = ""

  • エルビス演算子使用しない例
fun converterToHistory(items: List<HomeworkQuery.History_commit>?): String? {
    var jsonArray = JSONArray()
    items?.map {
        var jsonObject = JSONObject()
        jsonObject.put("content", if (it.content().isNullOrEmpty()) "" else it.content())
        jsonObject.put("finish_at", if (it.finished_at().isNullOrEmpty()) "" else it.finished_at())
        jsonObject.put("type", if (it.type().isNullOrEmpty()) "" else it.type())    
        jsonArray.put(jsonObject)
    }
    return jsonArray.toString()
}
  • エルビス演算子使用する例
fun converterToHistory(items: List<HomeworkQuery.History_commit>?): String? {
    var jsonArray = JSONArray()
    items?.map {
        var jsonObject = JSONObject()
        jsonObject.put("content", it.content() ?: "")
        jsonObject.put("finish_at", it.finished_at() ?: "")
        jsonObject.put("type", it.type() ?: "" )
        jsonArray.put(jsonObject)
    }
    return jsonArray.toString()
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?