Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
iOSアプリ開発にもDIツールを!
Search
haptaro
January 29, 2018
Programming
5
1.2k
iOSアプリ開発にもDIツールを!
Otemachi.swift × Kyobashi.swift #02
haptaro
January 29, 2018
Tweet
Share
More Decks by haptaro
See All by haptaro
通信環境が不安定でも心地よい体験を
kotaro
0
90
アニメーション実践入門
kotaro
3
580
はじめてのRxSwift体験記
kotaro
3
640
Swiftで気持ち良く開発したアプリを気持ち良くSubmitする
kotaro
0
7.2k
Other Decks in Programming
See All in Programming
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
From Translations to Multi Dimension Entities
alexanderschranz
2
130
Haze - Real time background blurring
chrisbanes
1
510
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
770
ゆるやかにgolangci-lintのルールを強くする / Kyoto.go #56
utgwkk
1
370
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
710
快速入門可觀測性
blueswen
0
340
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
710
testcontainers のススメ
sgash708
1
120
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
250
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Visualization
eitanlees
146
15k
A Philosophy of Restraint
colly
203
16k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Site-Speed That Sticks
csswizardry
2
190
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
810
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Code Review Best Practice
trishagee
65
17k
How GitHub (no longer) Works
holman
311
140k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Transcript
iOSΞϓϦ։ൃʹDIπʔϧΛʂ 2018/01/29 Otemachi.swift x Kyobashi.swift #02
ඌ ଠ (@haptaro) - Sansanגࣜձࣾ Eightࣄۀ෦ - ϞόΠϧΞϓϦΤϯδχΞ - iOSϝΠϯɺAndroidগͣͭ͠
2
લఏ ࣮ࡍʹಋೖͨ͠Θ͚Ͱͳ͍ɻ
લఏ ࣮ࡍʹಋೖͨ͠Θ͚Ͱͳ͍ɻ ݕ౼͍ͨ͠ʂ
ભҠઌͷActivityͷϑΟʔϧυʹ@InjectͬͯΞϊςʔγϣϯ͍ͭͯΔͰ͠ΐʁ Dagger͕ίʔυΛࣗಈੜͯ͠ϑΟʔϧυʹΛೖͯ͘͠ΕΔ͔Βɺࣗ ͰੜͤͣʹstartActivity͍͍ͤͯ͞Αʂ ಉ͡ػೳΛiOSͰ։ൃͨ͋͠ͱ AndroidͷίʔυϨϏϡʔʹͯ
!!! (iOSͷͱ͖ͳΜ͔৭ʑ࡞ͬͨͷʹ…)
͔֬ʹAndroidͰDaggerΑ͘ฉ͚͘Ͳɺ iOSͷDIπʔϧͲ͏ͳͷͩΖ͏
iOSͰDIπʔϧΛಋೖ͞Ε͍ͯΔํ
ຊ͓͢Δ͜ͱ - DIͱʁ - iOSΞϓϦ։ൃͰDIπʔϧΛ͏ͳΒ - ͦͷதͰDIKitͷ৮ͬͯΈͨ 9
ͦͦDI(Dependency Injection)ͱʁ
ґଘ͍ͯ͠ΔΦϒδΣΫτΛ֎͔ΒೖΕΔ σβΠϯύλʔϯ
ͨͱ͑ɺ ΦϒδΣΫτAͷΠϯελϯεԽʹΦϒδΣΫτBͱC͕ඞཁͰɺ ΦϒδΣΫτBͷΠϯελϯεԽʹΦϒδΣΫτD͕ඞཁ A C B D
ΦϒδΣΫτάϥϑ: ΦϒδΣΫτͷґଘؔΛද͢πϦʔ A C B D
ͦΕͧΕΦϒδΣΫτΛ֎͔ΒೖΕΔʂ A C B D ϓϩτίϧ
ͦΕͧΕΦϒδΣΫτΛ֎͔ΒೖΕΔʂ A C B D
ͲͷΑ͏ʹΦϒδΣΫτΛೖΕΔͷ͔ʁೖΕํ 16 ɾΠχγϟϥΠβInjectionɾɾɾinit()ܦ༝ͰΦϒδΣΫτΛೖΕΔɻҰ൪Α ͘ݟ͔͚Δ͕ɺViewControllerAppDelegateʹ͑ͳ͍ ɾϓϩύςΟInjectionɾɾɾϓϩύςΟʹΦϒδΣΫτΛೖΕΔ ɾϝιουInjectionɾɾɾϝιουܦ༝ͰΦϒδΣΫτΛೖΕΔ
DI͕͋ΔͱԿ͕خ͍͠ͷʁ
- ΦϒδΣΫτͷґଘؔΛΫϥεͷ֎ʹग़ͤΔͷͰɺϞδϡʔ ϧੑ্͕͕Γ࠶ར༻Ͱ͖Δ - ίϯϙʔωϯτಉ͕࢜ૄ݁߹ʹͳΓมߋʹॊೈʹͳΔ - ΦϒδΣΫτͷࠩ͠ସ͕͑Մೳʹ (ϢχοτςετͷࡍʹϞοΫΦϒδΣΫτͷࠩ͠ସ͑ͳͲ) 18
- DIʮґଘ͍ͯ͠ΔΦϒδΣΫτΛ֎͔ΒೖΕΔ୯ͳΔσβΠ ϯύλʔϯʯ - ΦϒδΣΫτͷೖΕํʹछྨ͕͋Δ 19
ͱ͍͑ɺґଘ͕ؔ૿͖͑ͯͯखಈͰґ ଘΦϒδΣΫτΛDI͍ͯ͘͠ͷ͔…
ͦ͜ͰDIαϙʔτπʔϧ✨
iOSΞϓϦͰ͑ͦ͏ͳDIαϙʔτπʔϧ(Swift) - Swinjectɾɾɾ@yoichitgy͞Μ͕࡞ऀɻDIίϯςφɻݱࡏͷiOSͷDI πʔϧͷσϑΝΫτελϯμʔυʁ - DIKitɾɾɾ@ishkawa͞Μ͕࡞ऀɻ͝ຊਓ͕iOSDC 2017Ͱհ - CleanseɾɾɾSquare 22
ґଘؔͷղܾͷλΠϛϯά 23 ɾಈతDIɾɾɾ࣮ߦ࣌ʹґଘؔͷղܾ ex. Swinject ɾ੩తDIɾɾɾίϯύΠϧ࣌ʹґଘؔͷղܾ ex. DIKit, Cake PatternΛ༻͍ͨDI,
ReaderϞφυΛ༻͍ͨDI
੩తDIಈతDIΑΓԿ͕ྑ͍ͷʁ - ҆શ: ίϯύΠϧ࣌ͷґଘؔͷղܾ͕͞ΕΔͷͰɺສ͕Ұؒҧ͍ͬͯͯ ίϯύΠϧΤϥʔʹͳΓɺ࣮ߦ࣌Ϋϥογϡ͛Δ - ࣮ߦ࣌গ͠ૣ͘: ґଘؔΛղܾͨ͠ίʔυ͕ίϯύΠϧ࣌ʹͰ͖Δͷ Ͱ࣮ߦ࣌ͷΛ্͛ΒΕΔ ͨͩ͠ɺنʹΑͬͯίϯύΠϧ͕࣌ؒ͘ͳΔͱ͍͏σϝϦοτ͋Δ
͔ 24
Swift҆શͳݴޠɻ ͬͺΓίϯύΠϧ࣌ʹґଘؔͷղܾΛ ͍ͨ͠
ίϯύΠϧ࣌ʹίʔυΛࣗಈੜͯ͠ґଘؔ ΛղܾͰ͖ΔDIKitͷExample৮ͬͯΈͨ
ґଘؔΛఆٛ
ґଘؔΛఆٛ
ΠϯελϯεΛఏڙ͢Δ provideϝιουΛఆٛ
Dependency͔ΒϏϧυ࣌ʹ ґଘؔΛղܾ͢ΔίʔυΛࣗಈੜ
Storyboardܦ༝ͷViewController ͷΠϯελϯεԽʹରԠ
Storyboardܦ༝ͷViewController ͷΠϯελϯεԽʹରԠ
ͲͷΑ͏ʹґଘؔΛղܾ͍ͯ͠Δͷ͔ 33 JavaͷΑ͏ʹΞϊςʔγϣϯ͕Swiftʹͳ͍ͷͰϓϩτί ϧΛϚʔΫͱͯ͠༻͠ɺSourceKittenͰίʔυղऍɾੜ ੜ͞ΕͨίʔυͰґଘؔΛղܾ
DIKit৮ͬͯΈͯ - Ϗϧυ࣌ʹίʔυ͕ࣗಈੜ͞ΕΔͷͰɺ࿙Ε͕͋ΔͱίϯύΠϧ ΤϥʔʹͳΔͷͰΘ͔Γ͍͢ - Storyboardܦ༝ͰͷViewControllerΠϯελϯεԽͰ͖Δ - Dagger2SwinjectʹൺΔͱ͔ͳΓγϯϓϧ (ٯʹείʔϓͳͲͷ֓೦ࠓͳ͘ɺDIίϯςφͱ·Ͱ͍͔ͳ͍) 34
·ͱΊ ɾSwiftͳΒͬͺΓίϯύΠϧ࣌ʹґଘؔΛ ղܾ͍ͨ͠ ɾDIKitγϯϓϧͰಋೖίετͦ͏ ɾࠓޙDIKitͷϦϙδτϦWatch͍ͯ͜͠͏
ͨͩ…
ɾͦͦϓϩμΫτͷنෳࡶੑɺϢχοτςετͷ ༗ແʹΑͬͯDIαϙʔτπʔϧΛೖΕͣ·ͣखಈDIͰ ྑ͍͔ ɾDIαϙʔτπʔϧʹΑͬͯػೳͷࠩҟ͕͋ΔͷͰԿ͕ద ͍ͯ͠Δ͔அ͕ඞཁ
DIαϙʔτπʔϧΛ͍ͬͯΔํ ࠙ձʹͰ͓ฉ͔ͤͯԼ͍͞
Your business network
ࢀߟࢿྉ - 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