Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
今日から使える! Optionalをいい感じに扱うtips
Search
PKPK-Carnage
March 19, 2019
Programming
0
1.2k
今日から使える! Optionalをいい感じに扱うtips
PKPK-Carnage
March 19, 2019
Tweet
Share
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
430
アプリへの導線の増やし方.pdf
pkpkcarnage
0
680
「シェア機能」について考えてみた
pkpkcarnage
0
98
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
190
Any型をprotocolにキャストする時に 気をつけた方がいい話
pkpkcarnage
0
270
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
300
iOSの通信処理を簡潔に書く
pkpkcarnage
0
260
Other Decks in Programming
See All in Programming
Missing parts when designing and implementing Android UI
ericksli
0
370
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
0
160
Functional Event Sourcing using Sekiban
tomohisa
0
130
競技プログラミングで 基礎体力を身につけよう / You can get basic skills through competitive programming
mdstoy
0
130
Leverage LLMs in Java with LangChain4j and Quarkus
hollycummins
0
110
Djangoの開発環境で工夫したこと - pre-commit / DevContainer
hiroki_yod
1
540
Remix on Hono on Cloudflare Workers
yusukebe
1
390
デザインパターンで理解するLLMエージェントの作り方 / How to develop an LLM agent using agentic design patterns
rkaga
11
2.3k
Cursorでアプリケーションの追加開発や保守をどこまでできるか試したら得るものが多かった話
drumnistnakano
0
240
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
9
4k
TypeScript でバックもやるって実際どう? 実運用で困ったこと3選
yuichiro_serita
17
6.9k
CSC509 Lecture 13
javiergs
PRO
0
130
Featured
See All Featured
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
1
210
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Statistics for Hackers
jakevdp
796
220k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Typedesign – Prime Four
hannesfritz
40
2.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
A Tale of Four Properties
chriscoyier
156
23k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Transcript
ࠓ͔Β͑Δʂ OptionalΛ͍͍ײ͡ʹѻ͏tips 2019/3/19 ͋Δ͋ΔLTʙεϚϗΞϓϦ։ൃΤϯδχΞʙ Vol.3 PKPK-Carnage(Tomosuke Okada)
About me • iOSྺ2ͪΐͬͱ • Qiita → @PKPK-Carnage • Github
→ PKPK-Carnage • Twitter → @PKPK-Carnage
࠷ۙࢥͬͨ͜ͱɾɾɾ 3
4 OptionalΛ੍͢ΔऀSwiftΛ੍͢!!
tips1: switch 5
switch 6 enum TestType: Int { case hoge = 0
case fuga = 1 case piyo = 2 }
switch 7 func generateText(from index: Int) -> String { guard
let unwrappedType = TestType(rawValue: index) else { return "" } switch unwrappedType { case .hoge: return "Hoge" case .fuga: return "Fuga" case .piyo: return "Piyo" } }
case?ͱ.noneΛ͏ͱɾɾɾ 8
switch 9 func generateText(from index: Int) -> String { guard
let unwrappedType = TestType(rawValue: index) else { return "" } switch unwrappedType { case .hoge: return "Hoge" case .fuga: return "Fuga" case .piyo: return "Piyo" } }
switch 10 func generateText(from index: Int) -> String { switch
TestType(rawValue: index) { case .hoge?: return "Hoge" case .fuga?: return "Fuga" case .piyo?: return "Piyo" case .none: return "" } }
switchͷఆର͕nilͩͬͨ߹ͷॲཧΛ .noneʹهड़Ͱ͖Δʂ 11
tips2: Nil Coalescing Operator + Closure 12
Nil Coalescing Operator 13 let text = "" let intValue
= Int(text) == nil ? 0 : Int(text)!
Nil Coalescing Operator 14 let text = "" let intValue
= Int(text) ?? 0
ཁ͢Δʹࡾ߲ԋࢉࢠͷলུ 15
??ͷޙʹClosureࢦఆͰ͖Δ 16
Nil Coalescing Operator 17 let text = "" let intValue
= Int(text) ?? 0
Nil Coalescing Operator + Closure 18 let text = ""
let intValue = Int(text) ?? { return 0 }()
͜ΕͰ͖ͯԿʹͳΔͷʁ 19
׆༻๏ 20 let text = "" let intValue = Int(text)
?? { return 0 }()
׆༻๏ 21 let text = "" let intValue = Int(text)
?? { assertionFailure("ࣈͷςΩετ͕ೖͬͯ·ͤΜʂ") return 0 }()
σόοάϏϧυͷ͚࣌ͩnilͩͬͨ࣌ʹ Ϋϥογϡͤ͞ΒΕΔ 22
·ͱΊ • optionalͷΦϒδΣΫτΛswitchʹ͢Δͱ͖ɺcase? ͱ.noneΛ͏ͱΞϯϥοϓ͢Δඞཁ͕ͳ͍ • ??ͷޙʹClosureΛೖΕΔ͜ͱ͕Ͱ͖ΔͷͰɺ͔ͳΓࣗ༝ ͕ߴ͍ɻ 23
͓ΘΓ 24