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

Step up Kotlin

Step up Kotlin

Paraya

July 13, 2017
Tweet

More Decks by Paraya

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ ϓϥοτϑΥʔϜ - Android Java 2೥ - iOS 3೥ -

    Android Kotlin 1೥ ※New! ͦͷଞ - DDD 2೥
  2. KotlinͬͯͲ͏ಈ͍ͯΔͷʁ // StringͷnameΛએݴ var name : String? name?.toInt() // Javaม׵ޙͷίʔυ

    if (name != null) { name.toInt(); } OVMMνΣοΫ͕ࣗಈੜ੒͞ΕΔ
  3. ϓϥΠϚϦίϯετϥΫλΛ ࢖Θͳ͍ class KotlinUser { val userId: String constructor(userId: String)

    { this.userId = userId } } αϒίϯετϥΫλͰॳظԽ Ϗϧυ͸௨Γ·͢ ˞XBSOJOH͸ग़·͢
  4. KotlinΛJavaΆ͘ॻ͘ fun function(text: String?) { text?.let { it.toInt() } }

    ͸/VMMBCMFUZQF /VMMBCMFUZQFΛࢀর͢Δࡍ͸ MFU ౳Ͱ/PO/VMMUZQFͱͯ͠ࢀর͢Δ ඞཁ͕͋Δ
  5. ?.letΛ࢖Θͳ͍ fun function(text: String?) { if (text != null) {

    text.toInt() } } /VMMBCMFUZQFΛࢀর͢Δࡍ͸ OVMMͰͳ͍จ຺Ͱ͋Ε͹0,
  6. lazy private View view; View getView() { if (this.view ==

    null) { return View.inflate(context, R.layout.view_top, null); } else { return this.view }
 }
  7. lazy private View view; View getView() { if (this.view ==

    null) { return LayoutInflater.from(context).inflate(R.layout.view_top, null); } else { return this.view }
 } private val view by lazy { LayoutInflater.from(context).inflate(R.layout.view_top, null); } MB[ZΛ༻͍Δ͜ͱͰɺϓϩύςΟࢀর࣌ʹॳظԽ͞Ε͍ͯ ͳ͍৔߹͚ͩॲཧΛߦ͏ͱ͍͏͜ͱ͕Ͱ͖Δ
  8. apply val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI).apply {
 type = "image/*"


    } // ͜͜ͰԿ͔͠Βͷॲཧ context.startActivity(intent) BQQMZΛར༻͢Δ͜ͱͰจ຺্ॳظԽࡁ ΈJOUFOUΛར༻Ͱ͖Δ
  9. lateinit class View : RelativeLayout { private var name: String?

    = null // nullͰॳظԽ͠ͳ͍ͱΤϥʔ fun initView(name: String) { this.name = name } } ຊ౰͸OBNFΛ/POOVMMͱͯ͠ѻ͍͍͕ͨɺ7JFXͷॳ ظԽ͸ίϯετϥΫλͰ͸ͳ͘PO$SFBUF ͳͲ "OESPJEͷϥΠϑαΠΫϧʹͳΔͷͰΤϥʔʹͳͬͯ ͠·͏
  10. lateinit class View : RelativeLayout { private lateinit var name:

    String fun initView(name: String) { this.name = name } } MBUFJOJUΛ෇͚Δ͜ͱͰɺॳظԽ͕ίϯετϥΫλ֎ ͰߦΘΕΔ͜ͱΛ໌ࣔͰ͖Δɻ ͨͩ͠ɺॳظԽ͠๨Εͨ৔߹͸ 6OJOJUJBMJ[FE1SPQFSUZ"DDFTT&YDFQUJPO͕ൃੜ