iOSアプリ開発にもDIツールを!

Adbff2a1447fbe53ca24a6943b3d80e4?s=47 haptaro
January 29, 2018

 iOSアプリ開発にもDIツールを!

Otemachi.swift × Kyobashi.swift #02

Adbff2a1447fbe53ca24a6943b3d80e4?s=128

haptaro

January 29, 2018
Tweet

Transcript

  1. iOSΞϓϦ։ൃʹ΋DIπʔϧΛʂ 2018/01/29 Otemachi.swift x Kyobashi.swift #02

  2. ෱ඌ ޾ଠ࿠ (@haptaro) - Sansanגࣜձࣾ Eightࣄۀ෦ - ϞόΠϧΞϓϦΤϯδχΞ - iOSϝΠϯɺAndroid΋গͣͭ͠

    2
  3. લఏ
 ࣮ࡍʹಋೖͨ͠Θ͚Ͱ͸ͳ͍ɻ

  4. લఏ
 ࣮ࡍʹಋೖͨ͠Θ͚Ͱ͸ͳ͍ɻ ݕ౼͍ͨ͠ʂ

  5. ભҠઌͷActivityͷϑΟʔϧυʹ@InjectͬͯΞϊςʔγϣϯ͍ͭͯΔͰ͠ΐʁ
 Dagger͕ίʔυΛࣗಈੜ੒ͯ͠ϑΟʔϧυʹ஋Λ୅ೖͯ͘͠ΕΔ͔Βɺࣗ෼ Ͱੜ੒ͤͣʹstartActivity͍͍ͤͯ͞Αʂ ಉ͡ػೳΛiOSͰ։ൃͨ͋͠ͱ
 AndroidͷίʔυϨϏϡʔʹͯ

  6. !!!
 (iOSͷͱ͖͸ͳΜ͔৭ʑ࡞ͬͨͷʹ…)

  7. ͔֬ʹAndroidͰ͸DaggerΑ͘ฉ͚͘Ͳɺ iOSͷDIπʔϧ͸Ͳ͏ͳͷͩΖ͏

  8. iOSͰDIπʔϧΛಋೖ͞Ε͍ͯΔํ

  9. ຊ೔͓࿩͢Δ͜ͱ - DIͱ͸ʁ - iOSΞϓϦ։ൃͰDIπʔϧΛ࢖͏ͳΒ - ͦͷதͰDIKitͷ৮ͬͯΈͨ 9

  10. ͦ΋ͦ΋DI(Dependency Injection)ͱ͸ʁ

  11. ґଘ͍ͯ͠ΔΦϒδΣΫτΛ֎͔ΒೖΕΔ
 σβΠϯύλʔϯ

  12. ͨͱ͑͹ɺ
 ΦϒδΣΫτAͷΠϯελϯεԽʹ͸ΦϒδΣΫτBͱC͕ඞཁͰɺ
 ΦϒδΣΫτBͷΠϯελϯεԽʹ͸ΦϒδΣΫτD͕ඞཁ A C B D

  13. ΦϒδΣΫτάϥϑ: ΦϒδΣΫτͷґଘؔ܎Λද͢πϦʔ A C B D

  14. ͦΕͧΕΦϒδΣΫτΛ֎͔ΒೖΕΔʂ A C B D ϓϩτίϧ

  15. ͦΕͧΕΦϒδΣΫτΛ֎͔ΒೖΕΔʂ A C B D

  16. ͲͷΑ͏ʹΦϒδΣΫτΛೖΕΔͷ͔ʁೖΕํ 16 ɾΠχγϟϥΠβInjectionɾɾɾinit()ܦ༝ͰΦϒδΣΫτΛೖΕΔɻҰ൪Α ͘ݟ͔͚Δ͕ɺViewController΍AppDelegateʹ͸࢖͑ͳ͍ ɾϓϩύςΟInjectionɾɾɾ௚઀ϓϩύςΟʹΦϒδΣΫτΛೖΕΔ ɾϝιουInjectionɾɾɾϝιουܦ༝ͰΦϒδΣΫτΛೖΕΔ

  17. DI͕͋ΔͱԿ͕خ͍͠ͷʁ

  18. - ΦϒδΣΫτͷґଘؔ܎ΛΫϥεͷ֎ʹग़ͤΔͷͰɺϞδϡʔ ϧੑ্͕͕Γ࠶ར༻Ͱ͖Δ - ίϯϙʔωϯτಉ͕࢜ૄ݁߹ʹͳΓมߋʹॊೈʹͳΔ - ΦϒδΣΫτͷࠩ͠ସ͕͑Մೳʹ
 (ϢχοτςετͷࡍʹϞοΫΦϒδΣΫτͷࠩ͠ସ͑ͳͲ) 18

  19. - DI͸ʮґଘ͍ͯ͠ΔΦϒδΣΫτΛ֎͔ΒೖΕΔ୯ͳΔσβΠ ϯύλʔϯʯ - ΦϒδΣΫτͷೖΕํʹ΋छྨ͕͋Δ 19

  20. ͱ͸͍͑ɺґଘؔ܎͕૿͖͑ͯͯ΋खಈͰґ ଘΦϒδΣΫτΛDI͍ͯ͘͠ͷ͔…

  21. ͦ͜ͰDIαϙʔτπʔϧ✨

  22. iOSΞϓϦͰ࢖͑ͦ͏ͳDIαϙʔτπʔϧ(Swift) - Swinjectɾɾɾ@yoichitgy͞Μ͕࡞ऀɻDIίϯςφɻݱࡏͷiOSͷDI πʔϧͷσϑΝΫτελϯμʔυʁ - DIKitɾɾɾ@ishkawa͞Μ͕࡞ऀɻ͝ຊਓ͕iOSDC 2017Ͱ঺հ - CleanseɾɾɾSquare੡ 22

  23. ґଘؔ܎ͷղܾͷλΠϛϯά 23 ɾಈతDIɾɾɾ࣮ߦ࣌ʹґଘؔ܎ͷղܾ
 ex. Swinject
 ɾ੩తDIɾɾɾίϯύΠϧ࣌ʹґଘؔ܎ͷղܾ
 ex. DIKit, Cake PatternΛ༻͍ͨDI,

    ReaderϞφυΛ༻͍ͨDI
  24. ੩తDI͸ಈతDIΑΓԿ͕ྑ͍ͷʁ - ҆શ: ίϯύΠϧ࣌ͷґଘؔ܎ͷղܾ͕͞ΕΔͷͰɺສ͕Ұؒҧ͍ͬͯͯ΋ ίϯύΠϧΤϥʔʹͳΓɺ࣮ߦ࣌Ϋϥογϡ͸๷͛Δ - ࣮ߦ࣌΋গ͠͸ૣ͘: ґଘؔ܎Λղܾͨ͠ίʔυ͕ίϯύΠϧ࣌ʹͰ͖Δͷ Ͱ࣮ߦ࣌ͷ଎౓Λ্͛ΒΕΔ
 ͨͩ͠ɺن໛ʹΑͬͯ͸ίϯύΠϧ͕࣌ؒ௕͘ͳΔͱ͍͏σϝϦοτ΋͋Δ

    ͔΋ 24
  25. Swift͸҆શͳݴޠɻ ΍ͬͺΓίϯύΠϧ࣌ʹґଘؔ܎ͷղܾΛ ͍ͨ͠

  26. ίϯύΠϧ࣌ʹίʔυΛࣗಈੜ੒ͯ͠ґଘؔ܎ ΛղܾͰ͖ΔDIKitͷExample৮ͬͯΈͨ

  27. ґଘؔ܎Λఆٛ

  28. ґଘؔ܎Λఆٛ

  29. ΠϯελϯεΛఏڙ͢Δ provideϝιουΛఆٛ

  30. Dependency͔ΒϏϧυ࣌ʹ
 ґଘؔ܎Λղܾ͢ΔίʔυΛࣗಈੜ੒

  31. Storyboardܦ༝ͷViewController ͷΠϯελϯεԽʹ΋ରԠ

  32. Storyboardܦ༝ͷViewController ͷΠϯελϯεԽʹ΋ରԠ

  33. ͲͷΑ͏ʹґଘؔ܎Λղܾ͍ͯ͠Δͷ͔ 33 JavaͷΑ͏ʹΞϊςʔγϣϯ͕Swiftʹ͸ͳ͍ͷͰϓϩτί ϧΛϚʔΫͱͯ͠࢖༻͠ɺSourceKittenͰίʔυղऍɾੜ੒ ੜ੒͞ΕͨίʔυͰґଘؔ܎Λղܾ

  34. DIKit৮ͬͯΈͯ - Ϗϧυ࣌ʹίʔυ͕ࣗಈੜ੒͞ΕΔͷͰɺ࿙Ε͕͋ΔͱίϯύΠϧ ΤϥʔʹͳΔͷͰΘ͔Γ΍͍͢ - Storyboardܦ༝ͰͷViewControllerΠϯελϯεԽ΋Ͱ͖Δ - Dagger2΍Swinjectʹൺ΂Δͱ͔ͳΓγϯϓϧ
 (ٯʹείʔϓͳͲͷ֓೦͸ࠓ͸ͳ͘ɺDIίϯςφͱ·Ͱ͸͍͔ͳ͍) 34

  35. ·ͱΊ ɾSwiftͳΒ΍ͬͺΓίϯύΠϧ࣌ʹґଘؔ܎Λ ղܾ͍ͨ͠ ɾDIKit͸γϯϓϧͰಋೖίετ͸௿ͦ͏ ɾࠓޙ΋DIKitͷϦϙδτϦWatch͍ͯ͜͠͏

  36. ͨͩ…

  37. ɾͦ΋ͦ΋ϓϩμΫτͷن໛΍ෳࡶੑɺϢχοτςετͷ ༗ແʹΑͬͯ͸DIαϙʔτπʔϧΛೖΕͣ·ͣ͸खಈDIͰ ΋ྑ͍͔΋ ɾDIαϙʔτπʔϧʹΑͬͯػೳͷࠩҟ͕͋ΔͷͰԿ͕ద ͍ͯ͠Δ͔൑அ͕ඞཁ

  38. DIαϙʔτπʔϧΛ࢖͍ͬͯΔํ
 ࠙਌ձʹͰ΋͓࿩ฉ͔ͤͯԼ͍͞

  39. Your business network

  40. ࢀߟࢿྉ - DIKit: https://github.com/ishkawa/DIKit - ishkawa͞Μͷϒϩά: http://blog.ishkawa.org/2017/09/18/1505701774/ - iOSΞϓϦͷઃܭͱDependency Injection:

    https://speakerdeck.com/ yonekawa/iosapurifalseshe-ji-todependency-injection - Swinject: https://github.com/Swinject/Swinject - Dependency Injection in Swift 2.x: https://niconare.nicovideo.jp/watch/kn1091 40