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
API Design in Swift
Search
FUJI Goro
June 20, 2014
Programming
7
1.7k
API Design in Swift
#WWDCPAD の発表資料です
http://connpass.com/event/6877/
FUJI Goro
June 20, 2014
Tweet
Share
More Decks by FUJI Goro
See All by FUJI Goro
How to Boost Your Code with WebAssembly
gfx
2
2.6k
AssemblyScriptでライブラリコードの高速化をしてみる
gfx
5
2.8k
実践TypeScriptトークバトル
gfx
1
1k
歴史的経緯の説明 as code
gfx
7
2.6k
Elasticsearchによる 全文検索の実装 in Rails
gfx
5
9.3k
すばらしきGraphQLのSEKAIへようこそ
gfx
20
9.1k
マルチテナント・ウェブアプリケーションの実践
gfx
14
9.4k
How to choose the ORM on Android
gfx
1
4.1k
How Do We Get Along With Static Types
gfx
5
3.3k
Other Decks in Programming
See All in Programming
サーバーレスで負荷試験!Step Functions + Lambdaを使ったk6の分散実行
shuntakahashi
6
1.6k
Some more adventure of Happy Eyeballs
coe401_
2
190
rails_girls_is_my_gate_to_join_the_ruby_commuinty
maimux2x
0
200
仮想ファイルシステムを導入して開発環境のストレージ課題を解消する
segadevtech
2
550
A New Era of Testing
mannodermaus
2
500
Debugging: All you need to know (for simultaneous interpreting)
jmatsu
2
790
Crafting Cross-Platform Adventures: Building a Game Engine with Kotlin Multiplatform
dwursteisen
0
120
Findy - エンジニア向け会社紹介 / Findy Letter for Engineers
findyinc
4
90k
GraphQL あるいは React における自律的なデータ取得について
quramy
11
3k
KSPの導入・移行を前向きに検討しよう!
shxun6934
PRO
0
270
Amazon Neptuneで始める初めてのグラフDB ー グラフDBを使う意味を考える ー
satoshi256kbyte
2
260
Architecture Decision Record (ADR)
nearme_tech
PRO
1
690
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
93
5.1k
The Cost Of JavaScript in 2023
addyosmani
42
5.7k
Done Done
chrislema
180
16k
Learning to Love Humans: Emotional Interface Design
aarron
270
40k
Why Our Code Smells
bkeepers
PRO
334
56k
The Pragmatic Product Professional
lauravandoore
31
6.2k
The Invisible Customer
myddelton
119
13k
Rebuilding a faster, lazier Slack
samanthasiow
78
8.6k
Debugging Ruby Performance
tmm1
72
12k
Become a Pro
speakerdeck
PRO
22
4.9k
Speed Design
sergeychernyshev
22
430
Being A Developer After 40
akosma
84
590k
Transcript
API Design In Swift #WWDCPAD FUJI Goro (@__gfx__), a software
engineer in COOKPAD WWDC2014 reports at COOKPAD, 2014/6/20
FUJI Goro (@__gfx__) • Software engineer @ COOKPAD Inc. •
Development common libraries for Android / iOS
We are hiring! https://info.cookpad.com/jobs/
Comparison of Swift and Objective-C
Swiftݴޠ༷ͷৄࡉʹ৮Ε·ͤΜ
SwiftͱObjective-Cͷൺֱ • ΞΫηε੍ޚ • ϝιουݺͼग़͠ߏจ • Φʔόʔϩʔυͱ໊લ͖Ҿ • ૯শతϓϩάϥϛϯά
ΞΫηε੍ޚ • Objective-Cެ։ϝιουͷΈϔομϑΝΠϧʹ ॻ͘ • ϑΟʔϧυʹΞΫηε͢Δͷݪଇېࢭ • Swift͕ͯ͢publicͰϑΟʔϧυΞΫηεࣗ༝ • ΞΫηείϯτϩʔϧΛ͢Δʹinterfaceͱ࣮Ϋ
ϥεΛ͚Δ͜ͱͰରԠͰ͖Δ͕࣮ίετ͕ߴ͍
ϝιουݺͼग़͠ߏจ • Objective-C: [[receiver method1] method2] • Swift: receiver.method1().method2() •
ϝιουνΣΠϯ͕Γ͍͢ • ࠷ޙͷҾ͕ؔܕͩͱಛघͳߏจ͋Γ • “trailing closures” • receiver.method() { params-> Type in … }
Φʔόʔϩʔυͱ໊લ͖Ҿ • γάωνϟ͕ҧ͑ಉ໊͡લͷϝιουΛෳ ఆٛͰ͖Δ • ύϥϝλͷܕ͕ҧ͑OK • Ҿͷܕ͕ҧͬͯOK • ໊લ͖ΦϓγϣφϧҾఆٛՄೳ
૯শతϓϩάϥϛϯά • ܕύϥϝʔλΛͱΔΫϥεϝιουΛఆٛ Ͱ͖Δ • δΣωϦΫεͱ͔ςϯϓϨʔτͱ͔͍ΘΕ Δͷ • ͨͱ͑ Array<T>
ʮҙͷܕTͷྻʯ
ͦͷଞ • Ϋϥεϝιουʢstatic methodʣଘࡏ͠ͳ͍ • ൃදͰఆٛͰ͖ͳ͍ͱݴͬͯ͠·͍·͕ͨ͠ɺ class funcͰఆٛͰ͖·͢ʢthanks to @corosuke_kʣ
• ԋࢉࢠΦʔόʔϩʔυ͕͋Δ • nilΛσϑΥϧτͰڐ༰͠ͳ͍ • nilڐ༰ܕ T? ͱ T!
Swiftʹ͋Δ੍ݶ • ϦϑϨΫγϣϯAPI • ͦΕΒ͖͠API͋ΔͷͰɺਖ਼نϦϦʔε· ͰʹՃ͞ΕΔՄೳੑ͋Γ • performSelectorܥͷಈతϝιουݺͼग़͠ • ObjCʹexportͨ͠Ϋϥεʹରͯ͠ͷΈՄೳ
Interoperability of Swift and Objective-C
՝ • Objective-CΞϓϦͰSwiftϥΠϒϥϦΛ͑ Δ͔ɺ͑Δͱͨ͠ΒͲ͏͍͏͕ඞཁ͔ • SwiftΞϓϦͰObjective-CϥΠϒϥϦΛ͑ Δ͔ɺ͑Δͱͨ͠ΒͲ͏͍͏͕ඞཁ͔
૬ޓӡ༻ͷݪଇ • Swift͔ΒObjective-Cͷݺͼग़͠: Մೳ • Objective-C͔ΒSwiftͷݺͼग़͠: Մೳ • ͍ͣΕઃఆ͕ඞཁ •
ΠϯλʔϑΣΠε100%ޓͰͳ͍
Objective-C from Swift • Objective-C APIΛSwift͔Β͏ͨΊͷϒϦο δXcode͕ࣗಈతʹੜͯ͘͠ΕΔ • Swift͔Βݟ͑ΔCocoa APIͷ΄ͱΜͲͦͷ
··͏͜ͱ͕Ͱ͖Δ • String, Array, DictionaryObjCͦͷ··
Blocks • ObjCͷblocksSwiftͷؔܕʹͳΔ • ϝιουؔͷ࠷ޙͷҾ͕blocksͩͱɺ trailing closuresߏจ͕͑Δ • RubyͰಋೖ͞Εͯ͘ΒΕΔΑ͏ʹͳͬ ͨϒϩοΫϝιουߏจͱ͓ͳ͡
Example func dispatch_async(queue: dispatch_queue_t!, block: dispatch_block_t!) ! // trailing closures
(ඌΫϩʔδϟʣ ! dispatch_async(dispatch_get_main_queue()) { NSLog(“foo”) } ! // ௨ৗͷߏจͰ͑Δ ! dispatch_async(dispatch_get_main_queue(), { NSLog("foo") }) !
Objective-CͷͨΊͷSwift • SwfitͰఆٛͨ͠ΫϥεΛObjective-CͰ͏ͨ ΊͷϒϦοδXcode͕ࣗಈੜ͢Δ • @objc ΞϊςʔγϣϯΛ͚ͭΔɺ·ͨ NSObjectΛܧঝͨ͠Ϋϥε͕ࣗಈతʹObjC ଆʹexport͞ΕΔ
ҙ • ϝιουͷΦʔόʔϩʔυԋࢉࢠΦʔόʔ ϩʔυͳͲɺObjCͰ࣮ݱͰ͖ͳ͍ػೳΤΫ εϙʔτͰ͖ͳ͍ • ʮObjCରԠSwiftϥΠϒϥϦʯಛผͷରԠ͕ ඞཁͰɺͦΕʹΑͬͯSwiftຊདྷͷύϑΥʔϚ ϯεΛൃشͰ͖ͳ͍Մೳੑ͕͋Δ
API Design
Swift࣌ͷAPIઃܭ • SwiftObjective-CΑΓڧྗ͕ͩɺશ໘తʹ SwiftʹҠߦ͢Δʹ·͔͔ͩΔͩΖ͏ • ͜Ε͔ΒͷॏཁͳͷɺʮObjective-C͔ Β͑ΔSwiftϥΠϒϥϦʯ͓ΑͼʮSwiftͰ ͍͍͢Objective-CϥΠϒϥϦʯͩʂ
SwiftϥΠϒϥϦΛॻ͘ݪଇ • public classΛObjCʹexport͢Δ • 3จࣈϓϨϑΟΫεґવͱͯ͠ඞཁͱࢥΘΕΔ • export͢ΔclassͰ੍ݶ͕͋Δ͜ͱʹҙ • ΦʔόʔϩʔυΛ͑ͳ͍
• δΣωϦΫεΛ͑ͳ͍
Objective-CΛॻ͘ݪଇ • มߋͦΕ΄Ͳͳ͍ • Cocoaࢿ࢈Λͦͷ··͔ͭ͏ͨΊʹ͔ͳΓ͍ͯ͠Δҹ • ෳͷblocksΛड͚औΔͷͰͳ͘ɺ࠷ޙͷҾͰҰ͚ͭͩ blocksΛड͚औΔΑ͏ʹ͢Δ • ެ։APIͰͷϦϑϨΫγϣϯආ͚Δ
• ͨͱ͑SELΛड͚औͬͯϝιουΛൃߦ͢ΔͷͰͳ͘ɺ blocksΛ͏
ҙ • ਖ਼ن൛Ͱطଘͷ੍ݶʹ͍ͭͯมߋ͕͋Δͱ ࢥΘΕΔͷͰɺಛʹϦϑϨΫγϣϯϒϦο δ·ΘΓݱࡏͷ༷Λ৴͗͢͡ͳ͍΄͏͕ Α͍͔
Show Notes • Welcome Swift • The Swfit Programming Language
• Using Swift with Cocoa and Objective-C
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠