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
今日から使える! Optionalをいい感じに扱うtips
Search
PKPK-Carnage
March 19, 2019
Programming
0
1.3k
今日から使える! Optionalをいい感じに扱うtips
PKPK-Carnage
March 19, 2019
Tweet
Share
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
460
アプリへの導線の増やし方.pdf
pkpkcarnage
0
710
「シェア機能」について考えてみた
pkpkcarnage
0
110
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
220
Any型をprotocolにキャストする時に 気をつけた方がいい話
pkpkcarnage
0
290
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
330
iOSの通信処理を簡潔に書く
pkpkcarnage
0
300
Other Decks in Programming
See All in Programming
EMこそClaude Codeでコード調査しよう
shibayu36
0
250
CSC305 Lecture 09
javiergs
PRO
0
280
20251016_Rails News ~Rails 8.1の足音を聴く~
morimorihoge
2
610
実践Claude Code:20の失敗から学ぶAIペアプログラミング
takedatakashi
15
6.3k
iOSでSVG画像を扱う
kishikawakatsumi
0
140
Go言語の特性を活かした公式MCP SDKの設計
hond0413
1
350
デミカツ切り抜きで面倒くさいことはPythonにやらせよう
aokswork3
0
250
Foundation Modelsを実装日本語学習アプリを作ってみた!
hypebeans
0
120
bootcamp2025_バックエンド研修_WebAPIサーバ作成.pdf
geniee_inc
0
120
Flutterで分数(Fraction)を表示する方法
koukimiura
0
140
Pull-Requestの内容を1クリックで動作確認可能にするワークフロー
natmark
2
520
Writing Better Go: Lessons from 10 Code Reviews
konradreiche
0
3.6k
Featured
See All Featured
The Invisible Side of Design
smashingmag
302
51k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Building a Scalable Design System with Sketch
lauravandoore
463
33k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
9
920
Music & Morning Musume
bryan
46
6.9k
Raft: Consensus for Rubyists
vanstee
140
7.1k
Docker and Python
trallard
46
3.6k
Scaling GitHub
holman
463
140k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
630
Designing Experiences People Love
moore
142
24k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
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