Swift <-> Kotlin

Cf72ecd4105b5818fcabcc0c0aaeee22?s=47 jsugai
November 24, 2017
160

Swift <-> Kotlin

Cf72ecd4105b5818fcabcc0c0aaeee22?s=128

jsugai

November 24, 2017
Tweet

Transcript

  1. Swift Kotlin Mercari, Inc 㱺 㱺

  2. About me Jun Sugai @calciolife Mercari


  3. Calcio? • ΠλϦΞޠͰαοΧʔͷ͜ͱ • 00೥͝Ζ͔ΒAC Millan ͷϑΝϯ • ηϦΤA Ϯν

  4. ൵ใ • ΠλϦΞ୅ද 60೥ͿΓ ʹ
 World Cup ग़৔Λಀ͢ • WC༧બഊୀ

    • ਖ਼௚ɺɺ࢓ํͳ͍ɻબख૚͕... >
  5. Agenda • KotlinͱSwiftΛோΊͳ͕Βࢥ͏ • Swift Ͱײ͡Δָ͠Έํ
 ˞ iOSͷ࿩͸͠·ͤΜ

  6. ຊ೔͸ Swift ॳֶͷΤϯδχΞ ͱͯ͠࿩͠·͢ ຐ࠯ङͳ͍͛ͯͩ͘͞

  7. ஌Γ͍ͨ Ͳ͏΍ͬͨΒ։ൃܥͷ৘ใΛݟ͚ͭΒΕΔͷ͔? •ॳֶ͔Βগ͠੒௕ͯ͘͠Δͱײ͡ΔṖͷয૩ײ
 StoryBoard ͷ༗ແ΍؅ཧํ๏, Segue ΍ View पΓͳͲ
 ઃܭͷࠜװʹؔΘΓͦ͏ͳͱ͜Ζ͸ɺௐ΂ͯ΋݁ہओྲྀ͕෼͔Βͳ͍

    •໨ʹͭ͘໰୊ͷଟ͘શ෦͸ Swift Ͱ͸ͳ͘ iOS ͕ݪҼ
 Swift ͷ໰୊͸ίϯύΠϧ͙࣌ؒΒ͍ʁʢίϯύΠϥͷख़੒౓ʣ
 iOS ͷ໰୊͸ۀΛഎෛ͏͔͠ͳ͍ʁ
  8. Swift ͷྑͦ͞͏ͩͱࢥͬͨॴ • શମతʹ্͔ΒԼʹॲཧΛΩϨΠʹهड़͕Ͱ͖ΔΑ͏ ͳ࢓૊Έ΍࢓ֻ͚͕ॆ࣮ͯ͠Δ(Α͏ʹײ͡Δɻࣗ෼͚ͩ?) • ඪ४ͳ੍ޚߏจҎ֎Ͱ nil/null ͰຒΊࠐΈͦ͏ͳ஍ཕ Λɺݴޠ࢓༷ͷํͰճආ͢Δज़͕ଟ͍

    • ਖ਼௚ willset/didset ͸িܸΛड͚ͨ
 ݟͯΔଆͩͱɺત·ͯ͘͠ࢮʹͦ͏
 ࢖͏ଆʹͳΔͱɺ࢖͍Ͳ͜Ζ͕೉ͯ͘͠ࢮʹͦ͏
  9. ٙ໰͕͋Δ ࣗ෼͕ Swift ͷੌ͘ྑ͍ͱײ͡Δ͜ͱ͕ɺ͋·Γ๙ΊΒ Ε͍ͯͳ͍Α͏ʹࢥ͏ Optional ͸͔ͨ͠ʹૉ੖Β͍͚͠Ͳ Optional ΛɺͲ͏ ͍͏ΞΠσΞͰ࣮ݱ͍ͯ͠Δ͔ͷ΄͏͕ؾʹͳΔ

    ஗ԆධՁͷ࢓૊Έ͸Ͳ͏ͳͬͯΔͷʁͱ͔ɻ
  10. KotlinͰ͸೉͍͠ Swiftͷָ͠Έํ

  11. ָ͠Έ 1. ݴޠ࢓༷͔ΒಡΈऔΔઓུ 2. ݴޠ࢓༷͔Βໝ૝͢Δڍಈ 3. Swift Evolution ͔Βۭ૝͢Δະདྷ૾ ࠓ೔ओʹ࿩͢಺༰

    ͍͔ͭ࿩͍ͨ͠಺༰
  12.  Kotlin ͸ Bytecode ܦ༝ͳͷͰໝ૝͕͸͔ͲΒͳ͍

  13. Swift ҹ৅ ɾධՁઓུ
 ɾࢀরͱ஋ ɾΫϥεͱϝιου

  14. ධՁઓུ •ա৒ͳ֦ுੑͷ཈੍ •Optional ͸࡞༻తॱংͷൣғͰఏڙ͞ΕΔ
 ݴޠ࢓༷͔Β෦෼ධՁΛ࡟আͨ͠ͷ͸ӳஅ •֦ுੑʹର͢Δҹ৅͸ʮ࢓༷ΛίϯύΫτʹ ͢Δ͜ͱͰݎ࿚ੑΛ૿͢ʯ
 άϩʔόϧม਺ͷ࣮૷ͱ͔

  15. ʘ ?P? ʗ

  16. ධՁઓུ:άϩʔόϧม਺ v_localOne = F_globalVar(); https://railsware.com/blog/2014/06/11/global-variables-in-swift-are-not-variables/ @HMPCBMJOJU@GVOD SwiftͰͷάϩʔόϧม਺͸λΠϓίϯςΩετͷ֎ଆʹ഑ஔ͞ΕΔɻʢͱυΩϡϝϯτʹॻ͍ͯ͋Δʣ
 C lang తʹߟ͑ΔͳΒ

    longjmp ͢ΔͱϨδελΛॻ͖׵͑ͨ৔߹ʹϩʔΧϧม਺είʔϓ͸஋ͷר͖໭͕͠
 อূ͞ΕΔ(auto) ͕άϩʔόϧม਺͸ר͖໭͞Εͳ͍ʹະఆٛͷ஋ͱͳΔ࣮૷͕ଟ͍ɻ
 Thread Context Λ Switch ͢Δͨͼʹ Global ม਺ͷר͖໭͠Λߦ͏ʹ͸ɺ෭࡞༻Λ൐͏͜ͱ͕ଟ͘SwiftͰ͸άϩʔόϧ ม਺ͷॳظԽΛؔ਺ݺͼग़͠ʹஔ͖׵͑Δ͜ͱͰ setjmp/longjmp Ͱͷ࣮ߦίʔυΛύλʔϯԽͯ͠࠷దԽޮ཰ΛߴΊͯ ͍Δʁ https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html#//apple_ref/doc/uid/TP40014097-CH14-XID_382 ಡ·ͳͯ͘0,
  17. ධՁઓུ:accessor Swift ͷΞΫηα΍ಛघͳػೳ(mutating ͱ͔) Subscripts ͸ίϯύΠϧ(ߏจղੳʣͰద੾ʹ ॲཧ͞ΕΔ 
 ͞Ε͍ͯͨ ASTߏจղੳͷ్தͰॲཧ͕ଘࡏ͢Δ


    Parse ͷ createSetterAccessorArgument Ͱߦ͍ͬͯΔ EJETFUXJMMTFU
  18. ධՁઓུ: •ΞΫηα͸ࣝผࢠ(Identifire)ʹରͯ͠෇༩͞ΕΔ •ࣝผࢠ(Identifire)ͱ͸ࢀরΛ൑அ͢ΔͨΊͷ໊લ • didset/willset ͸ mutating ͳͲͱಉ͡λΠϛϯάͰίϯύΠϥ ͕ϝιουΛੜ੒͢Δʢ಺෦తʹϝιουΛੜ੒͢Δʣ •

    ม਺ͷΞΫηαϒϩοΫ 
 OBNFTQBDF\ TUSVDU"DDFTTPST\ 4UPSBHF,JOE,JOE 'VOD%FDM (FUOVMMQUS  4FUOVMMQUS  .BUFSJBMJ[F'PS4FUOVMMQUS 'VOD%FDM "EESFTTOVMMQUS  .VUBCMF"EESFTTOVMMQUS 'VOD%FDM 8JMM4FUOVMMQUS  %JE4FUOVMMQUS ^ ^FOEBOPOZNPVTOBNFTQBDF
  19. ධՁઓུ: •ϓϩτίϧʹΑΔΫϥεͷ֦ு͸ґଘ/ܧঝؔ܎ͷෳࡶ͞Λ௿ ݮͤ͞Δ •·ͨ҆શͳࢀরΛอޢ͢ΔͨΊͷ໋ྩؒॱংͷॏෳΛ๷͗ݺ ͼग़͠ઌͷಛఆ͕༰қ •ϝιουΛల։͢ΔύΠϓϥΠϯ͸ίϯύΠϧ࣌ͷ࠷దԽର ৅ʹ͓͚ΔॏཁͳϙΠϯτ
 Statement ͸ Cold

    Block ͱ͠ɺ৞ΈࠐΈͳͲ͸ݸผͷ࠷దԽύΠϓϥΠϯͰॲཧ͍ͯ͠Δ
 Optimize Desine ͸ Swift/Doc/OptimizedDesign.md ʹ΋આ໌͕͋Δ
  20. ʘ ?P? ʗ

  21. ಛʹݴޠ࢓༷తͳҧ͍͔Βށ࿭͏Օॴ ɾϓϩύςΟपΓ(willset/didset) ɾSwift ͷ guard ͍͍ͬ͢Ͷ! ɾUIKit ਏ͍

  22. ྆ํΛ൓෮ԣ௓ͼ͠ͳ͕Βࢥ͏͜ͱ

  23. willset/didset

  24. ށ࿭͏͜ͱ Property Λॻ͖׵͑Δલ/ޙͰݺ͹ΕΔ؂ࢹػೳ ...
 var age: Int = 0 {

    willSet { // <- age ʹ஋Λ୅ೖ͢Δ࣌ʹݺ͹ΕΔ println("age willSet:\(age) -> \(newValue)") } didSet { // <- age ʹ஋͕୅ೖ͞Εͨޙʹݺ͹ΕΔ println("age didSet :\(oldValue) -> \(age)") } }
 ... ศར͕ͩ Kotlin Ͱ͸... custom setter/getter ͋ͨΓ͕མͱ͠ॴ? 
 ͨͩ Kotlin Ͱؤுͬͯදݱ͔ͨ͠Βͱݴͬͯར༻཰͕ߴ͍ͱ΋ࢥ͑ͳ͍ɻ
 ͜͏͍͏ͷ͸ݴޠ࢓༷Ͱදݱ͞ΕΔͱ࠷ߴͳػೳͷ୅දɻ ͨͩɺਖ਼௚͍ͬͯϕετϓϥΫςΟε͕෼͔Βͳͯ͘ɺ࢖ͬͯͳ͍/࢖͑ͯͳ͍....
 ϕετϓϥΫςΟε୭͔ڭ͑ͯɻҒ͍ਓɻΤϩ͍ਓʂ
  25. swift : guard statement

  26. 
 func hoge() { guard let name = nameField.text else

    {
 return
 }
 guard let addr = addrField.text else {
 return
 }
 sendLog(LOG_TYPE_FIELD, name, addr)
 ... }

  27. to Kotlin

  28. 
 func hoge() { val name = nameField?.text ?:return val

    addr = addrField?.text ?:return sendLog(LOG_TYPE_FIELD, name, addr)
 ... }

  29. ͕͔ͩ͠͠ɻବ՛ࢠ՛ࢠɻ
 Kotlin ͷ Elvis Operator ͸ϩʔΧϧ
 είʔϓͷϒϩοΫΛ࡞Βͳ͍ͷͰ
 ૣظ return Ҏ֎ͷॲཧ͕ඞཁͩͱ...

  30. 
 func hoge() { guard let name = nameField.text else

    {
 showMessage("please input name field") // local scope Λͭ͘ΕΔ͔Β return ͷલʹ΋ॲཧok!
 return
 }
 ... }
 
 func hoge() { if( nameField?.text == null ) { // ݹయతͳํ๏
 showMessage("please input name field") 
 return
 }
 ... }

  31. Kotlin Ͱ΋ Swift Έͨ͘ॻ͖͍ͨ!

  32. private inline fun <T> T?.checkNull(ifNull: ()-> T ): T =

    this ?: ifNull()
 
 func hoge() { nameField?.text.checkNull { showMessage("please input name field") return }
 ... }

  33. None
  34. Swift ͷྑ͍৘ใΛ ڭ͍͑ͯͩ͘͞!