Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
470
アプリへの導線の増やし方.pdf
pkpkcarnage
0
720
「シェア機能」について考えてみた
pkpkcarnage
0
110
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
220
Any型をprotocolにキャストする時に 気をつけた方がいい話
pkpkcarnage
0
300
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
330
iOSの通信処理を簡潔に書く
pkpkcarnage
0
310
Other Decks in Programming
See All in Programming
AIコーディングエージェント(Gemini)
kondai24
0
250
著者と進める!『AIと個人開発したくなったらまずCursorで要件定義だ!』
yasunacoffee
0
150
LLMで複雑な検索条件アセットから脱却する!! 生成的検索インタフェースの設計論
po3rin
4
900
The Art of Re-Architecture - Droidcon India 2025
siddroid
0
120
TestingOsaka6_Ozono
o3
0
170
実は歴史的なアップデートだと思う AWS Interconnect - multicloud
maroon1st
0
240
Tinkerbellから学ぶ、Podで DHCPをリッスンする手法
tomokon
0
140
SwiftUIで本格音ゲー実装してみた
hypebeans
0
460
FluorTracer / RayTracingCamp11
kugimasa
0
240
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.3k
Python札幌 LT資料
t3tra
6
990
クラウドに依存しないS3を使った開発術
simesaba80
0
120
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
Mind Mapping
helmedeiros
PRO
0
36
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
160
Done Done
chrislema
186
16k
The Art of Programming - Codeland 2020
erikaheidi
56
14k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
286
14k
How to Ace a Technical Interview
jacobian
281
24k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
140
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
180
The Power of CSS Pseudo Elements
geoffreycrofte
80
6.1k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
130
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