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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
51
スクラム完全理解
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
コンテキストの使い捨てをやめる — ビジネスルール駆動開発と miko —
ioki
0
210
Oxcを導入して開発体験が向上した話
yug1224
4
320
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
410
Honoでのサプライチェーン侵害対策 〜 3つのライブラリに学ぶ
yusukebe
6
1.3k
Java × distroless で 軽量なコンテナイメージを / Java on Distroless
contour_gara
0
550
New "Type" system on PicoRuby
pocke
1
960
Webフレームワークの ベンチマークについて
yusukebe
0
170
技術記事、AIに書かせるか、自分で書くか? 〜それでも私が自分の手で書く理由〜 / #QiitaConference
jnchito
2
1.4k
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
120
A2UI という光を覗いてみる
satohjohn
1
140
TAKTでAI駆動開発の品質を設計する
j5ik2o
7
1.3k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
580
Featured
See All Featured
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Site-Speed That Sticks
csswizardry
13
1.2k
Designing for Performance
lara
611
70k
Evolving SEO for Evolving Search Engines
ryanjones
0
220
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
420
Everyday Curiosity
cassininazir
0
230
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.2k
Practical Orchestrator
shlominoach
191
11k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
950
Sam Torres - BigQuery for SEOs
techseoconnect
PRO
0
290
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
600
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ͱ͍͏Ṗͷղܾࡦ ͦΜͳʹίʔυ͘ͳΒͳͯ͘൵͍͠