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
700
「シェア機能」について考えてみた
pkpkcarnage
0
100
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
210
Any型をprotocolにキャストする時に 気をつけた方がいい話
pkpkcarnage
0
280
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
320
iOSの通信処理を簡潔に書く
pkpkcarnage
0
290
Other Decks in Programming
See All in Programming
技術同人誌をMCP Serverにしてみた
74th
1
390
High-Level Programming Languages in AI Era -Human Thought and Mind-
hayat01sh1da
PRO
0
540
エラーって何種類あるの?
kajitack
5
310
ruby.wasmで多人数リアルタイム通信ゲームを作ろう
lnit
2
280
GraphRAGの仕組みまるわかり
tosuri13
8
500
すべてのコンテキストを、 ユーザー価値に変える
applism118
2
890
PicoRuby on Rails
makicamel
2
110
Is Xcode slowly dying out in 2025?
uetyo
1
210
AIプログラマーDevinは PHPerの夢を見るか?
shinyasaita
1
170
GoのGenericsによるslice操作との付き合い方
syumai
3
690
Rubyでやりたい駆動開発 / Ruby driven development
chobishiba
1
460
NPOでのDevinの活用
codeforeveryone
0
420
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
299
21k
How STYLIGHT went responsive
nonsquared
100
5.6k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Docker and Python
trallard
44
3.4k
Designing for Performance
lara
609
69k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
130
19k
[RailsConf 2023] Rails as a piece of cake
palkan
55
5.6k
The Straight Up "How To Draw Better" Workshop
denniskardys
234
140k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
The Pragmatic Product Professional
lauravandoore
35
6.7k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
124
52k
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