Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
20180410_iOSLT_SwiftとProtocol-OrientedProgramming
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
shtnkgm
April 10, 2018
Programming
140
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
20180410_iOSLT_SwiftとProtocol-OrientedProgramming
shtnkgm
April 10, 2018
More Decks by shtnkgm
See All by shtnkgm
Combine入門
shtnkgm
2
330
Property Wrappers
shtnkgm
0
380
Saliency Detection
shtnkgm
0
92
パフォーマンス改善とユニットテスト
shtnkgm
4
1.8k
iOSのコードベースレイアウト
shtnkgm
2
850
20190117_iOSLT_CBLinSwift.pdf
shtnkgm
0
130
SwiftとFunctional Reactive Programming
shtnkgm
0
210
20180710_iOSLT_iOSでDarkModeを実装する
shtnkgm
0
130
20180220_iOSLT_Swiftとオブジェクト間の通知のパターン
shtnkgm
0
180
Other Decks in Programming
See All in Programming
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
290
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
120
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
320
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
150
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
3
1.4k
當我們開始不讀 Code - AI Agent 時代,Code Review 與 Clean Code 還剩下什麼?
jame2408
1
360
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
210
Modernizing iOS Apps
seyfoyun
0
150
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
6
1.4k
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
1
170
JIS keyboard support in Omarchy
lef237
0
180
Event is what happens
moro
2
3.6k
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
516
110k
How to Ace a Technical Interview
jacobian
280
24k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Into the Great Unknown - MozCon
thekraken
41
2.8k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
3.7k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
420
Tell your own story through comics
letsgokoyo
1
1.1k
Optimizing for Happiness
mojombo
378
71k
Why Your Marketing Sucks and What You Can Do About It - Sophie Logan
marketingsoph
0
460
Testing 201, or: Great Expectations
jmmastey
46
8.4k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
Facilitating Awesome Meetings
lara
57
7.2k
Transcript
SwiftͱProtocol-Oriented Programming 2018.4.10 Shota Nakagami / @shtnkgm
ຊͷΰʔϧ ϓϩτίϧࢦΛͬͯɺͬͱSwiftyͳΤϯδχΞʹ
"Swift is a protocol-oriented language. " — David Abrahams @WWDC2015
Google Trendsʮprotocol-oriented programmingʯ
ϓϩτίϧΦϦΤϯςουͬͯͳΜͩΖ͏ʁ !
Protocol-Oriented Programming: POP • ༁͢ͱϓϩτίϧࢦϓϩάϥϛϯά • ϓϩτίϧࢦϓϩάϥϛϯάϓϩάϥϛϯάύϥμΠϜͷ Ұͭ
ϓϩάϥϛϯάύϥμΠϜͬͯͳΜͩΖ͏ʁ !
Programming Paradigm • ϓϩάϥϛϯάΛ͢Δͱ͖ͷߟ͑ํɺํ๏ͷ͜ͱ • ҰͭͷݴޠͰෳͷύϥμΠϜʹྨ͞ΕΔ͜ͱ͋Δ ʢ= ϚϧνύϥμΠϜʣ
ϓϩάϥϛϯάύϥμΠϜͷҰྫ ϓϩάϥϛϯάύϥμΠϜ ֓ཁ खଓ͖ܕʢProceduralʣ ঢ়ଶΛ࣋ͪɺҰ࿈ͷॲཧΛؔʹ·ͱΊΔ ΦϒδΣΫτࢦʢObject-Orientedʣ σʔλৼΔ͍ΛΦϒδΣΫτʹ࣋ͨͤΔ ؔܕʢFunctionalʣ ঢ়ଶ෭࡞༻Λ࣋ͨͳ͍ؔΛΈ߹ΘͤΔ ϦΞΫςΟϒʢReactiveʣ
σʔλͷྲྀΕʢdata flowʣͱσʔλͷมԽͷ ʹண δΣωϦοΫʢGenericʣ σʔλܗࣜʹґଘ͠ͳ͍ ΞεϖΫτࢦʢAspect-Orientedʣ ԣஅతؔ৺ࣄͷʹண
iOSΞϓϦ։ൃϚϧνύϥμΠϜ • Objective-CɾɾɾΦϒδΣΫτࢦ • Swiftɾɾɾϓϩτίϧࢦ ʢΦϒδΣΫτࢦ&ؔܕ&δΣωϦοΫʣ • ಛʹɺUIKitΦϒδΣΫτࢦͱͷؔΘΓ͕ڧ͍
ΦϒδΣΫτࢦͬͯͳΜ͚ͩͬʁ
ΦϒδΣΫτࢦͷ͓͞Β͍
Object-Oriented Programming: OOP • σʔλϝιουΛ࣋ͬͨΦϒδΣΫτΛͭͬͯͦ͘ͷ૬ޓ࡞ ༻ͰγεςϜΛܗ࡞Δ • ΦϒδΣΫτࢦͷࡾݪଇ • ϙϦϞʔϑΟζϜʢଟଶੑʣ
• ΧϓηϧԽʢӅṭʣ • ܧঝ
UIKitͷΫϥε֊
None
ΦϒδΣΫτࢦͱϓϩτίϧࢦ
Swift Standard Framework΄΅ߏମͰߏ ओͳߏཁૉ ΦϒδΣΫτࢦ ϓϩτίϧࢦ ܕ ࢀরܕʢClassʣ ܕʢStruct/Enumʣ நԽ
Superclass Protocol →Obective-C࣌ͷΫϥεͷར༻͔Βߏମͷར༻͕த৺ʹ
ΦϒδΣΫτࢦͰʮಈʯΛσβΠϯͯ͠ΈΔ
͍ΖΜͳಈΫϥεΛͭ͘Γ·͢
class Fish { } class Cat { } class Dog
{ } class Bird { }
Ͳͷಈ͝ΜΛ৯Δ →ॲཧΛڞ௨Խ͍ͨ͠
class Fish { func eat() { /* something */ }
} class Cat { func eat() { /* something */ } } class Dog { func eat() { /* something */ } } class Bird { func eat() { /* something */ } }
Ϋϥεʹڞ௨ॲཧΛ࣮ͯ͠ܧঝ
class Animal { func eat() { /* something */ }
} class Fish: Animal { } class Cat: Animal { } class Dog: Animal { } class Bird: Animal { }
Ҡಈํ๏·ͱΊΒΕͦ͏ʁ
Ϋϥεʹ࣮ʁ
class Animal { func eat() { /* something */ }
func swim() { /* something */ } func run() { /* something */ } func fly() { /* something */ } } class Dog: Animal { } let dog = Dog() dog.fly()
class Animal { func eat() { /* something */ }
func swim() { /* something */ } func run() { /* something */ } func fly() { /* something */ } } class Dog: Animal { } let dog = Dog() dog.fly()
ۭඈͿ͍͵͕ൃੜʢόάʣ
Ҡಈํ๏ʹ͍ͭͯͷந֓೦Λ࣮
ΑΓ҆શͳϓϩάϥϜʹ class Animal { func eat() { /* something */
} } class RunAnimal: Animal { func run() { /* something */ } } class Dog: RunAnimal { } let dog = Dog() dog.run() // OK dog.fly() // ίϯύΠϧΤϥʔ
ʊਓਓਓਓਓਓਓਓਓʊ ʼɹಥવͷ༷มߋɹʻ ʉY^Y^Y^Y^Y^Y^Y^Yʉ
None
ӭ͛ͯΕΔΧΤϧʁ
None
ΫϥεଟॏܧঝΛαϙʔτ͍ͯ͠ͳ͍ // error: multiple inheritance from classes 'SwimAnimal' and 'RunAnimal'
class Frog: SwimAnimal, RunAnimal { }
Ϋϥεͷܧঝʹؔ͢Δᶃ • ୯Ұʢ୯७ʣܧঝ͔͠Ͱ͖ͳ͍ͷͰɺΫϥεઃܭ͕͍͠ ɹˠΫϥε͕ංେԽɺڽू͕͘ͳΓ͕ͪʢͪ͝Ό·ͥʣ
Ϋϥεͷܧঝʹؔ͢Δᶄ • Ϋϥεͷมߋ͕શͯͷܧঝઌΫϥεʹٴ͢Δ ɹ →όάͷݪҼɺΫϥεมߋ͠ʹ͍͘
Ϋϥεͷܧঝʹؔ͢Δᶅ • ந֓೦ΠϯελϯεԽͰ͖ͯ͠·͏ →όάͷݪҼ // ۩ΫϥεͷΠϯελϯεԽ let dog = Dog()
let frog = Frog() // நΫϥεͷΠϯελϯεԽ let animal = Animal() // ͲΜͳಈʁ
Ϋϥεͷܧঝʹؔ͢Δᶆ • ΦʔόʔϥΠυલఏͷϝιου class Animal { func bark() { //
ಈͱ͍͏֓೦ͳΜͯ໐͘?? // // } }
Ϋϥεͷܧঝʹؔ͢Δᶆ • ΦʔόʔϥΠυલఏͷϝιου class Animal { func bark() { //
ಈͱ͍͏֓೦ͳΜͯ໐͘?? // ΦʔόʔϥΠυ͞Εͳ͍߹Ϋϥογϡͤ͞Δ͔... fatalError() } } ܧঝઌͰͷ࣮࿙Ε͕͋Δͱόάͷݩʹ
ϓϩτίϧࢦͰʮಈʯΛσβΠϯͯ͠ΈΔ
Protocol Conformance ϓϩτίϧ४ڌʢద߹ʣ
ੑ࣭ΛϓϩτίϧͰදݱͯ͠४ڌ
protocol Eatable { func eat() } struct Cat: Eatable {
// eatϝιουͷ࣮͕ڧ੍͞ΕΔ // ίϯύΠϥͰνΣοΫ͞ΕΔͷͰ҆શ func eat() { print("ͺ͘ͺ͘") } }
Protocol Extension ϓϩτίϧΤΫεςϯγϣϯʢ֦ுʣ
ϓϩτίϧΤΫεςϯγϣϯͰσϑΥϧτ࣮ protocol Eatable { func eat() } extension Eatable {
func eat() { print("͙͙") } } struct Dog: Eatable { } let dog = Dog() dog.eat() // ͙͙
Protocol Composition ϓϩτίϧίϯϙδγϣϯʢ߹ʣ
ෳϓϩτίϧ४ڌͰΧΤϧղফ
struct Frog: Swimmable, Runnable { } let frog = Frog()
frog.swim() frog.run()
Protocol Inheritance ϓϩτίϧܧঝ
ϓϩτίϧܧঝՄೳ
protocol Movable { func move(method: String) } extension Movable {
func move(method: String) { print(method + "ਐΈ·͢") } } protocol Swimmable: Movable { func swim() } extension Swimmable { func swim() { move(method: "ӭ͍Ͱ") } } struct Fish: Swimmable { } let fish = Fish() fish.swim() // ӭ͍ͰਐΈ·͢
͞ΒʹFlyableΛಈͰͳ͍ܕͰ४ڌ • e.g. ඈߦػ • ඈͿͱ͍͏ੑ࣭͔Βઃܭ͢Δ͜ͱͰɺಈʹґଘ͠ͳ͍ • ॊೈͰ֦ுੑͷ͋ΔϓϩάϥϜʹͳΔ
ϓϩτίϧܕͰ͑Δ • ܕͷStructEnumʹΫϥεͷΑ͏ͳܧঝػೳͳ͍͕ɺϓ ϩτίϧʹ४ڌͰ͖Δ • ܕࢀরܕʢΫϥεʣΑΓ҆શ • Λڞ༗͠ͳ͍ͷͰҙਤ͠ͳ͍ͷมߋ͕ൃੜ͠ͳ͍ • εϨουηʔϑ
• ॥ࢀর͕ൃੜ͠ͳ͍
ϓϩτίϧܕͱͯ͠ѻ͑Δ • ϙϦϞʔϑΟζϜͰૄ݁߹ʹ ! func feed(eatable: Eatable) • ΠϯελϯεԽෆՄ !
// Error: Protocol type 'Eatable' cannot be instantiated let eatable = Eatable()
δΣωϦΫεʢ૯শܕʣ͑Δ ΫϥεStructͷΑ͏ͳܕύϥϝʔλ<T>͑ͳ͍͕ɺ associatedtypeͰ૯শܕΛఆٛͰ͖Δ class Eatable<FoodType> { } struct Eatable<FoodType> {
} protocol Eatable { associatedtype FoodType }
protocol Eatable { associatedtype FoodType func eat(food: FoodType) } struct
Bird: Eatable { typealias FoodType = Fish func eat(food: FoodType) { // do something } }
ϓϩτίϧࢦ͍͍Ͷ
·ͱΊ
࠷ڧͷϓϩάϥϛϯάύϥμΠϜͳ͍ • ͲΕղܾͷखஈͷҰͭʹ͗͢ͳ͍ • ϓϩτίϧࢦΛΔ͜ͱͰϓϩάϥϛϯάͷ෯͕͕Δ • ͍ΖΜͳํ๏ΛΓɺదࡐదॴ͑Δͷ͕࠷ڧ
ΦϒδΣΫτࢦ ϓϩτίϧࢦ σʔλͷ҉తڞ༗ ×ʢ͋Γʣ ◦ʢͳ͠ʣ நԽ ◦ʢSuperclassʣ ◦ʢProtocolʣ ίϯϙδγϣϯ ×ʢͰ͖ͳ͍ʣ
◦ʢProtocol Compositionʣ நܕͷΠϯελϯεԽ ×ʢͰ͖Δʣ ◦ʢͰ͖ͳ͍ʣ σϑΥϧτ࣮ ◦ʢSuperclassʣ ◦ʢProtocol Extensionʣ ۩ܕͷ࣮ͷڧ੍ ×ʢಈతνΣοΫʣ ◦ʢ੩తνΣοΫʣ ϙϦϞʔϑΟζϜ ◦ʢSuperclassʣ ◦ʢProtocolʣ δΣωϦΫε ◦ʢܕύϥϝʔλʣ ◦ʢassociatedtypeʣ UIKitΦϒδΣΫτͷܧঝ ◦ʢͰ͖Δʣ ×ʢͰ͖ͳ͍ʣ ϥΠϑαΠΫϧཧʢdeinitʣ ◦ʢ͋Γʣ ×ʢͳ͠ʣ Ұҙੑͷ͋Δσʔλʢsingletonʣ ◦ʢ࡞Γ͍͢ʣ ×ʢ࡞Γʹ͍͘ʣ
ΑΓৄ͘͠ • WWDC2015ηογϣϯಈը Protocol-Oriented Programming in Swift / WWDC2015 •
ެࣜϦϑΝϨϯε Protocols / The Swift Programming Language (Swift 4.1) • ॻ੶ Swift 4 Protocol-Oriented Programming - Third Edition
͓ΘΓ