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
Swift Generic Protocol
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Yuta Kawabe
December 06, 2017
Programming
43
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swift Generic Protocol
Yuta Kawabe
December 06, 2017
More Decks by Yuta Kawabe
See All by Yuta Kawabe
年末年始読んだ本LT
yaruki00
0
81
イマイチなsubscribeをRxぽくする
yaruki00
0
680
気をつけよう!認知バイアス
yaruki00
1
65
iOSDC2019トークピックアップ
yaruki00
0
50
スクラム完全理解
yaruki00
0
63
小さなチームでも高品質なモバイルアプリを作るためのテスト戦略
yaruki00
0
77
ここがダメだよ🍎さん!
yaruki00
0
43
Meeプロジェクト紹介
yaruki00
0
69
iOSDCまとめ
yaruki00
0
44
Other Decks in Programming
See All in Programming
RTSPクライアントを自作してみた話
simotin13
0
610
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
550
軽量Java基盤の設計 DIコンテナに頼らない、長期保守と1秒起動の実現 JJUG CCC 2026 Spring
macha64
0
540
AIとASP.NET Coreで雑Webアプリを作った話
mayuki
0
650
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.2k
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
Agentic UI
manfredsteyer
PRO
0
170
気圧・高度・GPSを記録&可視化するアプリ「Koudo」を作った話
hjmkth
1
270
Observability in Practice:Grafana 與 Edge Device SRE 的那些事
blueswen
0
170
Oxcを導入して開発体験が向上した話
yug1224
4
320
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
810
A2UI という光を覗いてみる
satohjohn
1
140
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
180
Marketing to machines
jonoalderson
1
5.5k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Bash Introduction
62gerente
615
220k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
62
54k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
160
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.7k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
610
The Language of Interfaces
destraynor
162
27k
Done Done
chrislema
186
16k
Transcript
Swift Generic Protocol 2017/12/6(ਫ) ΤϯδχΞษڧձ ล ༟ଠ
͡Ίʹ ςʔϚ͕LTͰѻ͏ʹॏ͍ ͦͷׂʹࢿྉ͕ࡶ ໌Β͔ʹ༨ܭͳͱ͜Ζʹ࿑ྗΛׂ͍͍ͯΔ ͝ΊΜͶʂ
Ϟνϕʔγϣϯ Wireframe(Routerϓϩτίϧ)Λڞ௨Խ͍ͨ͠ ֤ContractʹҎԼͷΑ͏ͳίʔυ HogeViewͷ෦֤ViewΛฦ͍ͨ͠ protocol HogeWireframe: class { weak var
view: HogeView? { get set } static func assembleModule() -> HogeView } Generics͡Όʂ
Genericͱ͍͑ classstruct, funcʹtype parameterΛ͚ͭΔ class SomeClass<T: Int> { var someProperty:
[T] = [] func someFunction<S>(arg0: T, arg1: S) { print(arg0, arg1) } } let c = SomeClass<Int>() // func ͷ߹type parameterΛ໌ࣔ͠ͳ͍ c.someFunction(arg0: 0, arg1: "hello")
protocolಉ༷ʹ…ʁ ͖ͬͱ͜Μͳײ͡Ͱ͠ΐʁ protocol SomeProtocol<T: Int> { var someProperty: [T] =
[] func someFunction<S>(arg0: T, arg1: S) } var p: SomeProtocol<Int>
None
classͷΑ͏ʹͰ͖ͳ͍ protocolͷએݴʹtype parameter͑ͳ͍ ͳΜͰ͔Α͘Θ͔Βͳ͍ Ͱgenericʹ͍ͨ͠͡ΌΜʁ
͡Ό͋Ͳ͏͢Μͷ͞ SelfΛ͏ protocolʹద߹͢ΔΫϥεͳΒ͜ΕͰ associatedtypeΛ͏ ͦͷଞͷΫϥεͪ͜Β
Self ద߹͢ΔΫϥεʹͳΔ classͰ͑Δ(ܧঝ͢Δͱ͖ͱ͔) protocol HasSelfProtocol { func returnSelf() -> Self
} class AdoptHasSelfClass: HasSelfProtocol { func returnSelf() -> Self { return self } } var hasSelfProtocol: HasSelfProtocol = AdoptHasSelfClass()
associatedtype ҙͷܕΛએݴͰ͖Δ protocol AssociatedTypeProtocol { associatedtype T var someProperty: T
{ set get } func someFunction() -> T } class AdoptAssociatedTypeClass: AssociatedTypeProtocol { typealias T = Int // ແͯ͘Α͍ var someProperty: Int = 0 func someFunction() -> Int { return someProperty } }
type parameterͱԿ͕ҧ͏ͷ͔ɾɾɾ
ͦΕͱ͔͘ Generic ProtocolΛ࡞Δ͜ͱ͕Ͱ͖ͨʂ ޙ͜ΕΛϓϩύςΟͷܕʹͯ͠ɾɾɾ var associatedTypeProtocol: AssociatedTypeProtocol = AdoptAssociatedTypeClass()
িܸͷ৽ࣄ࣮ Generic ProtocolϓϩύςΟͷܕʹͰ͖ͳ͍ ύϑΥʔϚϯεతͳཧ༝Ͱ(ਪଌ) https://qiita.com/omochimetaru/items/b41e7699ea25a324aefa ٕज़తʹͰ͖ͳ͍͜ͱͳ͍Β͍͠
Type Erasureͱ͍͏ղܾࡦ ϓϩτίϧΛ࣮ࡍͷܕʹద༻ struct AnyAssociatedType<A: AssociatedTypeProtocol>: AssociatedTypeProtocol { typealias T
= A.T var someProperty: T private let _someFunction: () -> T func someFunction() -> T { return _someFunction() } init<Inner: AssociatedTypeProtocol>(_ inner: Inner) where T == Inner.T { self.someProperty = inner.someProperty self._someFunction = inner.someFunction } } var associatedTypeProtocol: AnyAssociatedType<AdoptAssociatedTypeClass> = AnyAssociatedType(AdoptAssociatedTypeClass())
݁ہͲ͏ͬͨͷʁ ͳΜ͔Ϋϥε࡞Γ·ͨ͠ protocol BaseViewWireframe: class { associatedtype SomeView: UIView weak
var view: SomeView? { get set } static func assembleModule(vc: UIViewController) -> SomeView } class HogeWireframe: BaseViewWireframe { weak var view: HogeViewImpl? class func assembleModule(vc: UIViewController) -> HogeViewImpl { return HogeViewImpl() } }
·ͱΊ SwiftͷGeneric Protocol੍Ͱ͍ʹ͍͘ type parameter͑ͳ͍ ϓϩύςΟͷܕʹͰ͖ͳ͍ Type Erasureͱ͍͏Ṗͷղܾࡦ ͦΜͳʹίʔυ͘ͳΒͳͯ͘൵͍͠