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
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
90
パフォーマンス改善とユニットテスト
shtnkgm
4
1.8k
iOSのコードベースレイアウト
shtnkgm
2
840
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
170
Other Decks in Programming
See All in Programming
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
4.9k
contenteditable と日本語入力に向き合う
colorful12
0
190
RAG の “R” を Swift で覗いてみる 〜「意味から探す」検索の仕組み〜
nao_randd
0
160
sbt 2.0: takeoff
eed3si9n
0
150
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.4k
コーディング自動化の次へ。人の介入をゼロにするファクトリーとサポートAIによる実践
timakin
2
1.9k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
120
dotNET_vulnclicker_intro.pdf
deka_morita
0
140
Apple Intelligence を用いた個人情報誤送信防止、及びユーザーリクエスト体験の改善について
yukiny
0
340
The Rails Doctrine Decade
koic
2
570
ActionController meets Protocol Buffers
sorah
0
300
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
Featured
See All Featured
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
350
Building Adaptive Systems
keathley
44
3.2k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
First, design no harm
axbom
PRO
2
1.3k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
490
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Exploring anti-patterns in Rails
aemeredith
4
540
Bash Introduction
62gerente
615
220k
Statistics for Hackers
jakevdp
799
230k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
31k
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
͓ΘΓ