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.1k
今日から使える! Optionalをいい感じに扱うtips
PKPK-Carnage
March 19, 2019
Tweet
Share
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
380
アプリへの導線の増やし方.pdf
pkpkcarnage
0
600
「シェア機能」について考えてみた
pkpkcarnage
0
88
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
170
Any型をprotocolにキャストする時に 気をつけた方がいい話
pkpkcarnage
0
250
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
260
iOSの通信処理を簡潔に書く
pkpkcarnage
0
240
Other Decks in Programming
See All in Programming
pixivアプリでマルチモジュールを実現するまで
gatosyocora
1
130
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
8
3.7k
CQRS/ES avec Symfony, c’est (trop) bien !
jeremyfreeagent
1
630
Front-end application development, Symfony-style(s)
dunglas
2
1.9k
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
340
educure_カリキュラム生操作マニュアル.pdf
linew_official
0
490
甘い香りに誘われてVanilla Extractを1年間運用してみた
miyahkun
1
110
⼤規模⾔語モデルの拡張(RAG)が 終わったかも知れない件について
nearme_tech
22
15k
Elm Form Validation
bkuhlmann
0
510
Zero Waste, Radical Magic, and Italian Graft – Quarkus Efficiency Secrets
hollycummins
0
220
What We Can Learn From OSS
inouehi
0
400
Ruby Function Composition
bkuhlmann
1
330
Featured
See All Featured
How GitHub Uses GitHub to Build GitHub
holman
468
290k
Large-scale JavaScript Application Architecture
addyosmani
503
110k
For a Future-Friendly Web
brad_frost
171
8.9k
Navigating Team Friction
lara
177
13k
Raft: Consensus for Rubyists
vanstee
132
6.2k
Principles of Awesome APIs and How to Build Them.
keavy
120
16k
StorybookのUI Testing Handbookを読んだ
zakiyama
11
4.6k
From Idea to $5000 a Month in 5 Months
shpigford
377
45k
Writing Fast Ruby
sferik
620
60k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
501
140k
A Philosophy of Restraint
colly
196
16k
What's in a price? How to price your products and services
michaelherold
237
11k
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