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.7k
AssemblyScriptでライブラリコードの高速化をしてみる
gfx
5
2.9k
実践TypeScriptトークバトル
gfx
1
1.1k
歴史的経緯の説明 as code
gfx
7
2.7k
Elasticsearchによる 全文検索の実装 in Rails
gfx
5
9.4k
すばらしきGraphQLのSEKAIへようこそ
gfx
20
9.2k
マルチテナント・ウェブアプリケーションの実践
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
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
120
Jakarta EE meets AI
ivargrimstad
0
410
RubyLSPのマルチバイト文字対応
notfounds
0
110
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
Jakarta EE meets AI
ivargrimstad
0
480
as(型アサーション)を書く前にできること
marokanatani
7
2.5k
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
550
EventSourcingの理想と現実
wenas
6
2.3k
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
5
1.1k
subpath importsで始めるモック生活
10tera
0
270
CSC509 Lecture 09
javiergs
PRO
0
140
Featured
See All Featured
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Designing Experiences People Love
moore
138
23k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Navigating Team Friction
lara
183
14k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Unsuck your backbone
ammeep
668
57k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
110
RailsConf 2023
tenderlove
29
900
Art, The Web, and Tiny UX
lynnandtonic
297
20k
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
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠