Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
AWS CDKの推しポイントN選
akihisaikeda
1
210
オフライン対応!Flutterアプリに全文検索エンジンを実装する @FlutterKaigi2025
itsmedreamwalker
2
320
DSPy Meetup Tokyo #1 - はじめてのDSPy
masahiro_nishimi
1
100
関数の挙動書き換える
takatofukui
4
750
Evolving NEWT’s TypeScript Backend for the AI-Driven Era
xpromx
0
210
Honoを技術選定したAI要件定義プラットフォームAcsimでの意思決定
codenote
0
280
Duke on CRaC with Jakarta EE
ivargrimstad
0
300
Module Harmony
petamoriken
2
580
Atomics APIを知る / Understanding Atomics API
ssssota
1
220
Feature Flags Suck! - KubeCon Atlanta 2025
phodgson
0
180
最新のDirectX12で使えるレイトレ周りの機能追加について
projectasura
0
310
TVerのWeb内製化 - 開発スピードと品質を両立させるまでの道のり
techtver
PRO
3
1.3k
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.3k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
11
950
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.8k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
Rebuilding a faster, lazier Slack
samanthasiow
84
9.3k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.1k
Building Applications with DynamoDB
mza
96
6.8k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
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