Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
iOSアプリ開発にもDIツールを!
Search
haptaro
January 29, 2018
Programming
5
1.3k
iOSアプリ開発にもDIツールを!
Otemachi.swift × Kyobashi.swift #02
haptaro
January 29, 2018
Tweet
Share
More Decks by haptaro
See All by haptaro
通信環境が不安定でも心地よい体験を
kotaro
0
97
アニメーション実践入門
kotaro
3
610
はじめてのRxSwift体験記
kotaro
3
640
Swiftで気持ち良く開発したアプリを気持ち良くSubmitする
kotaro
0
7.3k
Other Decks in Programming
See All in Programming
All(?) About Point Sets
hole
0
230
AIと協働し、イベントソーシングとアクターモデルで作る後悔しないアーキテクチャ Regret-Free Architecture with AI, Event Sourcing, and Actors
tomohisa
2
10k
AWS CDKの推しポイントN選
akihisaikeda
1
210
[堅牢.py #1] テストを書かない研究者に送る、最初にテストを書く実験コード入門 / Let's start your ML project by writing tests
shunk031
11
6.2k
Rails Girls Sapporo 2ndの裏側―準備の日々から見えた、私が得たもの / SAPPORO ENGINEER BASE #11
lemonade_37
2
190
How Software Deployment tools have changed in the past 20 years
geshan
0
21k
TVerのWeb内製化 - 開発スピードと品質を両立させるまでの道のり
techtver
PRO
3
1.3k
レイトレZ世代に捧ぐ、今からレイトレを始めるための小径
ichi_raven
0
480
ソフトウェア設計の課題・原則・実践技法
masuda220
PRO
23
19k
Building AI Agents with TypeScript #TSKaigiHokuriku
izumin5210
5
1.1k
Microservices Platforms: When Team Topologies Meets Microservices Patterns
cer
PRO
1
660
Java_プロセスのメモリ監視の落とし穴_NMT_で見抜けない_glibc_キャッシュ問題_.pdf
ntt_dsol_java
0
230
Featured
See All Featured
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.6k
How to Think Like a Performance Engineer
csswizardry
28
2.3k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.3k
Designing for Performance
lara
610
69k
Mobile First: as difficult as doing things right
swwweet
225
10k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.3k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.2k
Building Applications with DynamoDB
mza
96
6.8k
Scaling GitHub
holman
464
140k
It's Worth the Effort
3n
187
29k
RailsConf 2023
tenderlove
30
1.3k
GitHub's CSS Performance
jonrohan
1032
470k
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