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

Swiftは"""    """のか?

Ba4af0fede46022fdb6f5da8819784f9?s=47 ukaznil
August 09, 2017

 Swiftは"""    """のか?

Swift/Kotlin勉強会( https://oi-study.connpass.com/event/62521/ )のLT資料です。

Ba4af0fede46022fdb6f5da8819784f9?s=128

ukaznil

August 09, 2017
Tweet

Transcript

  1. Swift͸ """͔Θ͍͍""" ͷ͔ʁ 2017/08/09 Wed. Swift/Kotlinษڧձ @ukaznil: Tatsuya KONISHI http://ukaznil.com/

  2. It's me !! • Android app dev • झຯͰ5೥ڧ •

    Kotlinྺ1.5೥ • iOS app dev • Swift2िؒ͘Β͍ • misc • web site: http://ukaznil.com/ , Twitter: @ukaznil • '17 ৽ଔ > γεςϜ։ൃ / AI Papyrus  ࠷ۙαΠτͷσβΠϯΛ৽͘͠͠·ͨ͠
  3. Agenda • ࣗݾ঺հ • ʮ(Java →) Kotlin → SwiftʯαΠυͰ͢ɻ •

    Kotlin͸"""◦◦◦◦"""ʂ • Swift͸"""◦◦◦◦"""ʁ • ·ͱΊ 
  4. @Android • Java → Kotlinͷ޾ͤ • JavaΑΓ؆қͳه๏ • ύϫϑϧͳ֦ுੑ •

    null safety • NPE͕ઈରʹى͖ͳ͍Θ͚͡Όͳ͍ɿ https://speakerdeck.com/ryotamurohoshi/puratutohomuxing-nituite-kotlindatutejian-wei-erutonulldehamaruyo • ʢޙൃݴޠΏ͑ͷʣؾ഑Γ͕ॆ࣮ • ଞͷݴޠ࣮૷Λࢀߟʹ͍ͯ͠Δ • ScalaɿίϯετϥΫλͱ͔val&varͱ͔ͦͬ͘Γ Kotlin͸"""͔Θ͍͍"""ʂ  4DBMBCMF-BOHVBHFͷུ "QBDIF4QBSLͳͲͱ૬ੑ͕Α͍ɼ෺ੌ͘ύϫϑϧͳ+7.ݴޠ
  5. Swift

  6. ͋Μͳʹ """͔Θ͍͍""" Kotlinͱ ࣅͯΔͬͯ ϗϯτ͔ʁ

  7. Swift͸ """͔Θ͍͍""" ͷ͔ʁ 2017/08/09 Wed. Swift/Kotlinษڧձ @ukaznil: Tatsuya KONISHI http://ukaznil.com/

  8. Swift͸ """͔Θ͍͍""" ͷ͔ʁ 2017/08/09 Wed. Swift/Kotlinษڧձ @ukaznil: Tatsuya KONISHI http://ukaznil.com/

    ,PUMJOɹɹΑΓ΋
  9. """͔Θ͍͍"""ͬͯͳΜͩʁ ʲ͔Θ͍͍ʳby ݱ୅ࠃޠྫղࣙయ ୈ4൛ — ਂ͍ѪΛײ͡ɼେ੾ʹͯ͠΍Γ͍ͨؾ࣋ͪʹͳΔɻ — ࢥΘͣඍসΈͨ͘ͳΔ΄ͲѪΒ͍͠ɻ — ·ͨɼখͯ͘͞ɼ΍͘͞͠ѻͬͯ΍Γ͍ͨؾ࣋ͪΛىͤ͜͞

    Δ͞·Ͱ͋Δɻ  — ʢJavaͱൺֱͯ͠ʣKotlinΛॻ͘ͷָ͕͍͠͞·ɻ·ͨɼͦ ΕΛՄೳʹ͢Δݴޠ࢓༷ͷɼۭؾΛಡΜͩػඍ͕ѪΒ͍͜͠ͱɻ
  10. ݸਓతKotlinͷศརͳݴޠ࢓༷ 1.੍ޚߏจ - if 2.੍ޚߏจ - when 3.ίϯετϥΫλ 4.จࣈྻల։ 5.null

    safety 6.Smart cast 7.data class 8.֦ுؔ਺ 9.είʔϓؔ਺  4XJGU͸,PUMJOͱಉ༷ͷ ͔Θ͍͞ΛৢͤΔ͔ʁ https://kotlinlang.org/docs/reference/ vs
  11. Agenda • ࣗݾ঺հ • ʮ(Java →) Kotlin → SwiftʯαΠυͰ͢ɻ •

    Kotlin͸"""͔Θ͍͍"""ʂ • Swift͸"""͔Θ͍͍"""ʁ • KotlinͷػೳΛSwiftͰ࠶ݱ͠Α͏ • ·ͱΊ 
  12. 1. ੍ޚߏจ - if // ifจ͸ʮࣜʯͰ͋Δ // ifࣗମ͕ฦΓ஋Λ࣋ͯΔ val a

    = 100 val b = 50 val max = if (a > b) { a } else { b } // ࡾ߲ԋࢉࢠ͸ଘࡏ͠ͳ͍ val min = if (a > b) b else a // Swiftͷif͸ࣜͰ͸ͳ͍ // Kotlinͷํ͕ศར let a = 100 let b = 50 let max: Int if (a > b) { max = a } else { max = b } // ࡾ߲ԋࢉࢠ͸͋Δ let min = (a > b) ? b : a 
  13. 2. ੍ޚߏจ - when // whenͰ͸஋͚ͩͰͳ͘ɼ // ൣғ΍ܕ΋ݕࠪͰ͖Δ val value:

    Int = 100 val message = when(value) { 100 -> "100" in 10..99 -> "in 10..99" !in 10..99 -> "!in 10..99" as Int -> { //ϒϩοΫͰ΋ॻ͚ΔΑ "Int" } else -> "hoge" } // SwiftͰ͸whenͰ͸ͳͯ͘switch // ൣғ΍ܕ΋ݕࠪͰ͖Δ let value: Int = 100 switch value { case (0 ... 50): print("this is in 10~50") case (51 ..< 100): print("this is in 51~99") case is Int: print("this is Int") print("this is over 99") default: break } 
  14. 3. ίϯετϥΫλ // ίϯετϥΫλҾ਺ʹval, varΛࢦఆͰ͖Δ // ࢦఆͨ͠΋ͷ͸ϓϩύςΟͱͯ͠ΞΫηεͰ͖Δ class Man(val name:

    String, var age: Int) { // ηΧϯμϦίϯετϥΫλ constructor(name: String): this(name, 0) val displayName: String // "init{ }"ͰΠχγϟϥΠζͰ͖Δ init { displayName = "${name}(${age})" } } // Πϯελϯεੜ੒ʹnew͸͍Βͳ͍ val man = Man("John Tytor", 30) println(man.name) // ϓϩύςΟԽ͞ΕͯΔ man.age++ // age͸varࢦఆͨ͠ͷͰมߋՄ // ίϯετϥΫλ͸KotlinΑΓ΋"JavaͬΆ͍" class Man { let name: String var age: Int init(name: String) { self.name = name self.age = 0 } // ηΧϯμϦίϯετϥΫλ init(name: String, age: Int) { self.name = name self.age = age } } // ଞ͸Kotlinͱ͋·ΓมΘΒͳ͍ let man = Man(name: "Kyoma", age: 18) print(man.name) man.age += 1 
  15. 4. จࣈྻల։ // Javaͩͱจࣈྻͷදݱ͕෺ੌ͘໘౗͍͘͞ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // Javaͷྫ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> String name

    = "Tatsuya KONISHI"; int age = 26; System.out.println( name + " is " + age + " years old"); // Kotlinͩͱ "$" ͑͞෇͚͓͚ͯ͹Կͱ͔ͳΔ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // Kotlinͷྫ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> val name = "i" val lang = "Kotlin" println( "${name.toLowerCase()} love $lang") // SwiftͰ΋ಉ͡Α͏ͳॻ͖ํ͕Ͱ͖Δ let entries = 40 let str = "ࠓ೔ͷࢀՃऀ͸\(entries)ਓͰ͢" print(str) 
  16. 5. null safety //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // non-nullͳString //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> val greeting: String

    = "Hello, Kotlin" println(greeting.length) //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // nullableͳString //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> val msg: String? = null // ίϯύΠϧΤϥʔʢNPEͷՄೳੑʣ println(msg.length) // NPE͸ى͖ͣɼ"null"ͱදࣔ͞ΕΔ println(msg?.toUpperCase()?.length) // ͓ΊͰͱ͏NPE println(msg!!.toUpperCase().length) || println((msg!!.toUpperCase()).length) //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // non-nullͳString //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> let greeting: String = "Hello, Swift" print(greeting.characters.count) //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // nullableͳString //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> let msg: String? = nil // ίϯύΠϧΤϥʔʢNPEͷՄೳੑʣ print(msg.characters.count) // NPE͸ى͖ͣɼ"nil"ͱදࣔ͞ΕΔ print(msg?.characters.count) // NPEͱ਌ີͳؔ܎ print(msg!.characters.count) 
  17. 6. Smart cast fun demo(x: String?) { // x:String?ͳͷͰ "?."

    ͕ඞཁ print(x?.length) if (x != null) { // ΩϟετࡁΈͳͷͰ "." ͚ͩͰΑ͍ print(x.length) } } // whenͱ૊Έ߹ΘͤΔͱ࠷ڧ val y: Any = ~~~ when (y) { is Int -> print(y + 1) is String -> print(y.length + 1) is IntArray -> { print(y.sum()) } } ============== // Javaͩͱɻɻɻ if(y instanceOf String) { // ͏ͻΌʔ print(((String) y).length + 1) } func demo(x: String?) { // x:String?ͳͷͰ "?." ͕ඞཁ print(x?.characters.count) if let X = x { // X: StringʹΩϟετࡁΈ print(X.characters.count) } } // switchͱ૊Έ߹ΘͤΔͱ࠷ڧʁʁ let y: Any = ~~~ switch y { case let Y as Int: print(Y + 1) case let Y as String: print(Y.characters.count + 1) default: break } 
  18. 7. data class // data class͸ɼ // ୯७ʹଐੑΛ͚࣋ͭͩͷΫϥεʢͰ͋Δ͜ͱ͕ଟ͍ʣ data class

    User(val name: String, val age: Int) // equals() ෆཁ // hashCode() ෆཁ // toString() ෆཁ ============== val jack = User(name = "Jack", age = 1) val olderJack = jack.copy(age = 2) // Destructing Declaration: ෼ղએݴ val (name, age) = jack println("$name, $age years of age") //============== // data class͑͞ϝϯυ΢ͳਓʹ͸ɼ // Pair(A, B)ͱ͔Triple(A, B, C)΋͋Δ͚Ͳɼ // ϓϩύςΟ͕"first"ͱ͔"second"ͳͷͰඇਪ঑ //============== ʻ֘౰ػೳͳ͠ʼ 
  19. 8. ֦ுؔ਺ // ϝιου fun String.repeat2(): String { return "$this$this"

    } // ϓϩύςΟ val String.Companion.hyphen: String { get() = { "-" } } extension String { // ϝιου func repeat2() -> String { return "\(self)\(self)" } // ϓϩύςΟ static var hyphen: String { return "-" } } 
  20. // KotlinͷՖܗɾείʔϓؔ਺ʹ͸ // let, with, run, apply, also ͕͋Δ //

    ྫ͑͹ let ͷఆٛ inline fun <T, R> T.let(f: (T) -> R): R { return f(this) } ============== // letͷΑ͋͘Δྫ // 1) foo == null ͳΒ͹ upperCase = null // 2) foo = "foo" ͳΒ͹ upperCase = "FOO" val upperCase: String? = foo?.let { it.toUpperCase() } ============== // applyͷΑ͋͘Δྫ val upperCase = "hoge".apply { this.toUpperCase() } 9. είʔϓؔ਺ ʻ֘౰ػೳͳ͠ʼ  blog: https://goo.gl/wFaVvN
  21. 9. είʔϓؔ਺ // KotlinͷՖܗɾείʔϓؔ਺ʹ͸ // let, with, run, apply, also

    ͕͋Δ // ྫ͑͹ let ͷఆٛ inline fun <T, R> T.let(f: (T) -> R): R { return f(this) } ============== // letͷΑ͋͘Δྫ // 1) foo == null ͳΒ͹ upperCase = null // 2) foo = "foo" ͳΒ͹ upperCase = "FOO" val upperCase: String? = foo?.let { it.toUpperCase() } ============== // applyͷΑ͋͘Δྫ val upperCase = "hoge".apply { this.toUpperCase() } val t1 = TextView(this@MainActivity).apply { this.setText("MOJI") this.text = "MOJI" text = "MOJI" } // ্ͷ3ͭͷॻ͖ํ͸શͯಉ͡ҙຯ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // ͬ͜ͱΓʔΜ̇ //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> val b = Button(this@MainActivity).apply { text = "do click !!" setOnClickListener { finish() } } //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> // ŢƄŒřƄōō //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Button b = new Button(MainActivity.this); b.setText("do click...") b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } });  blog: https://goo.gl/wFaVvN
  22. Agenda • ࣗݾ঺հ • ʮ(Java →) Kotlin → SwiftʯαΠυͰ͢ɻ •

    Kotlin͸"""͔Θ͍͍"""ʂ • Swift͸"""͔Θ͍͍"""ʁ • KotlinͷػೳΛSwiftͰ࠶ݱ͠Α͏ • ·ͱΊ 
  23. Kotlin << Swift ͳ෦෼ • ϝιου࣮ߦ࣌ʹҾ਺໊Λࢦఆ͢Δ • ϓϩύςΟΦϒβʔό͕͋Δ func greeting1(who:

    String) -> String { return "\(who), ͜Μʹͪ͸" } greeting1(who: "খௗ͞Μ") ============== func greeting2(_ who: String) -> String { return "\(who), ͜Μʹͪ͸" } greeting2("JetBrains") var ม਺໊: ܕ { willSet { ߋ৽௚લͷεςʔτϝϯτ } didSet { ߋ৽௚ޙͷεςʔτϝϯτ } }
  24. Agenda • ࣗݾ঺հ • ʮ(Java →) Kotlin → SwiftʯαΠυͰ͢ɻ •

    Kotlin͸"""͔Θ͍͍"""ʂ • Swift͸"""͔Θ͍͍"""ʁ • KotlinͷػೳΛSwiftͰ࠶ݱ͠Α͏ • KotlinΑΓ͔Θ͍͍ͱ͜Ζ΋͋ͬͨ ← NEW!! • ·ͱΊ 
  25. ·ͱΊ  Ͳͬͪ΋͔Θ͍͍ جຊࣅͯͨɻ

  26. Swift͸ """͔Θ͍͍""" ͷ͔ʁ 2017/08/09 Wed. Swift/Kotlinษڧձ @ukaznil: Tatsuya KONISHI http://ukaznil.com/

    ,PUMJOɹɹΑΓ΋
  27. Thank you Swifters & Kotlinists ʢݺͼํ෼͔Δਓڭ͍͑ͯͩ͘͞ʂʂʂʣ