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
89
パフォーマンス改善とユニットテスト
shtnkgm
4
1.8k
iOSのコードベースレイアウト
shtnkgm
2
840
20190117_iOSLT_CBLinSwift.pdf
shtnkgm
0
130
SwiftとFunctional Reactive Programming
shtnkgm
0
200
20180710_iOSLT_iOSでDarkModeを実装する
shtnkgm
0
130
20180220_iOSLT_Swiftとオブジェクト間の通知のパターン
shtnkgm
0
170
Other Decks in Programming
See All in Programming
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
260
MIZARU@SPAJAM2026 第二回予選
1901drama
0
110
スマート反転とウェブアクセシビリティ
camiha
0
180
RSSとCodexを使ってX投稿自動化してみた
ochtum
0
110
業務時間外もAIに働いてもらう話
colorful12
3
10k
LoopHub - ローカルで動く GitHub で、AI と共同開発
jugyo
1
490
Swift愛好会と私(ウホーイ) / Swift Fan Club and Uhooi
uhooi
0
150
kubernetes コンポーネント開発入門 / 新卒N年目の勉強会&交流会!〜〇〇への誘い〜 #n_study
mazrean
0
190
【DroidKaigi 2026】「アクセシビリティを利用するとき、 アクセシビリティもまたこちらを利用している」 〜マルウェアによる攻撃と防衛について〜
halunoyo
0
430
XP祭りでしか伝わらないフリップネタ #xpjug
murabayashi
0
120
デプロイ直後のレイテンシスパイクを調べたら、 Railsの仕様にたどり着いた
nhsykym
0
110
Foundry Localでエージェント開発
seosoft
0
160
Featured
See All Featured
Six Lessons from altMBA
skipperchong
29
4.5k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.7k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
460
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
280
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
360
30k
Making the Leap to Tech Lead
cromwellryan
135
10k
The Curious Case for Waylosing
cassininazir
1
500
Ten Tips & Tricks for a 🌱 transition
stuffmc
0
210
The Limits of Empathy - UXLibs8
cassininazir
1
650
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
500
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
45k
The agentic SEO stack - context over prompts
schlessera
0
920
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
͓ΘΓ