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.8k
AssemblyScriptでライブラリコードの高速化をしてみる
gfx
5
3k
実践TypeScriptトークバトル
gfx
1
1.1k
歴史的経緯の説明 as code
gfx
7
2.7k
Elasticsearchによる 全文検索の実装 in Rails
gfx
6
9.4k
すばらしきGraphQLのSEKAIへようこそ
gfx
20
9.2k
マルチテナント・ウェブアプリケーションの実践
gfx
14
9.5k
How to choose the ORM on Android
gfx
1
4.2k
How Do We Get Along With Static Types
gfx
5
3.3k
Other Decks in Programming
See All in Programming
ドメインイベント増えすぎ問題
h0r15h0
2
440
return文におけるstd::moveについて
onihusube
1
1.3k
Cloudflare MCP ServerでClaude Desktop からWeb APIを構築
kutakutat
1
580
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
540
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
120
Запуск 1С:УХ в крупном энтерпрайзе: мечта и реальность ПМа
lamodatech
0
450
PSR-15 はあなたのための ものではない? - phpcon2024
myamagishi
0
180
17年周年のWebアプリケーションにTanStack Queryを導入する / Implementing TanStack Query in a 17th Anniversary Web Application
saitolume
0
250
バグを見つけた?それAppleに直してもらおう!
uetyo
0
190
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
240
103 Early Hints
sugi_0000
1
260
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
The Cost Of JavaScript in 2023
addyosmani
46
7k
4 Signs Your Business is Dying
shpigford
182
21k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
The Pragmatic Product Professional
lauravandoore
32
6.3k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.5k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
66k
Agile that works and the tools we love
rasmusluckow
328
21k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
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
͝੩ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠