Upgrade to Pro — share decks privately, control downloads, hide ads and more …

kotlin season 1 - Part 6 Null Safety - 2

TaeHwan
October 07, 2016

kotlin season 1 - Part 6 Null Safety - 2

2016년 유데미 Kotlin 강의를 공개합니다.

영상 : https://www.youtube.com/watch?v=vbaC8L2SuFU

2회 분은 유데미에서 확인 가능합니다.
http://bit.ly/2UpaJUR

TaeHwan

October 07, 2016
Tweet

More Decks by TaeHwan

Other Decks in Education

Transcript

  1. Part 6 Kotlin Null Safety - 2 Taehwan 1 ରࣁ؀

    উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  2. ૑դदрী • উ੹ೠ NULL ୊ܻ • return null 2 ରࣁ؀

    উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  3. য়ט਷ • nullਸ return ߉૑ ঋب۾ ୊ܻೞӝ • if /

    else ؀न ࢎਊೞӝ • Null Pointer Exception • উ੹ೠ ഋ ߸ജ • FilterNotNull ੉ਊೞӝ 3 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  4. list੄ null ઁ৻ೞӝ - java List<String> itemList = new ArrayList<>();

    itemList.add("A"); itemList.add(null); itemList.add("B"); for (String text : itemList) { if (text != null) { Log.d("TAG", "Text : " + text); } } 5 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  5. list੄ null ઁ৻ೞӝ - kotlin val listWithNulls: List<String?> = listOf("A",

    null, "B") for (item in listWithNulls) { if (item != null) { println(item) } } 6 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  6. list੄ null ઁ৻ೞӝ - kotlin • ?.let {} ࠶۾ਸ ੉ਊೞݶ

    ৮੹ೠ nullਸ ߓઁೞҊ, ૊द чਸ ࢎਊೡ ࣻ ੓׮. 7 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  7. list੄ null ઁ৻ೞӝ - kotlin val listWithNulls: List<String?> = listOf("A",

    null, "B") for (item in listWithNulls) { item?.let { println(it) } // prints A and ignores null } 8 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  8. if / else ؀न ࢎਊೞӝ - java String temp =

    ""; int size = temp != null ? temp.length() : 0; 10 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  9. if / else ؀न ࢎਊೞӝ - kotlin var temp: String?

    = "" val size = if (temp != null) temp.length else 0 11 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  10. if / else ؀न ࢎਊೞӝ - kotlin var temp: String?

    = "" val size = temp?.length • Ѿҗ : length ژח null 12 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  11. if / else ؀न ࢎਊೞӝ • Elvis Operator ?: 13

    ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  12. if / else ؀न ࢎਊೞӝ ?: (Elvis Operator) var temp:

    String? = null val size = temp?.length ?: 0 • Ѿҗ : 0 ژח length 14 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  13. NPE ߊࢤदఃח ߑߨ - java String temp = null; int

    size = temp.length(); 16 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  14. NPE ߊࢤदఃח ߑߨ - kotlin • Elvis Operatorܳ ࢎਊೞח ߑߨ

    var temp: String? = null val size = temp?.length ?: throw NullPointerException("temp is null") 17 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  15. NPE ߊࢤदఃӝ - kotlin var temp: String? = null val

    size = temp!!.length 18 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  16. NPE ৘৻ ୊ܻ val temp: String? = null val size:

    Int try { size = temp!!.length } catch (e: Exception) { size = 0 } 20 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  17. NPE ৘৻ ୊ܻ - 1.0.4ࠗఠ val temp: String? = null

    val size = try { temp!!.length } catch (e: Exception) { 0 } print("size $size") 21 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  18. ௏ౣܽ উ੹ೠ ഋ߸ജ दبೞӝ val a: String? = "ABC" //

    Stringਸ ъઁ۽ ഋ ߸ജ ೡ ҃਋ীח CastException੉ ߊࢤೞৈ ੉ ҃਋ null੉ ੷੢ val aInt: Int? = a as? Int? // null੉ ইצ 0ਸ ੷੢ೞ۰ݶ val aInt: Int? = a as? Int? ?: 0 23 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016
  19. list੄ filterNotNull val nullableList: List<Int?> = listOf(1, 2, null, 4)

    for (i in nullableList) { print("${i} ") } println("") // null filter val intList: List<Int> = nullableList.filterNotNull() for (i in intList) { print("${i} ") } 25 ରࣁ؀ উ٘۽੉٘ ѐߊ ঱য ௏ౣܽ, द੘ೞӝ thdev.tech, 2016