Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
今日から使える! Optionalをいい感じに扱うtips
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
PKPK-Carnage
March 19, 2019
Programming
1.4k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
今日から使える! Optionalをいい感じに扱うtips
PKPK-Carnage
March 19, 2019
Transcript
24 slides
ࠓ͔Β͑Δʂ 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
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
530
アプリへの導線の増やし方.pdf
pkpkcarnage
0
760
「シェア機能」について考えてみた
pkpkcarnage
0
130
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
240
Any型をprotocolにキャストする時に 気をつけた方がいい話
pkpkcarnage
0
320
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
380
iOSの通信処理を簡潔に書く
pkpkcarnage
0
330
Other Decks in Programming
See All in Programming
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
330
SODA - FACT BOOK(EN)
sodainc
1
250
AI時代のTerminal環境を UNIX哲学で設計・運用する
yasunori0418
1
350
Spec-driven Development with AI
simas
PRO
0
2.3k
Crash Auto Pipelines
veronikapj
1
420
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
170
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
580
선언형 햅틱 라이브러리 진동을 소개합니다
l2hyunwoo
1
320
AIが実装を担う時代の「技術設計」と「チームマネジメント」
kamina_zzz
2
270
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
280
React Native에 현대적인 빌드 도구 더하기: Rollipop 구현기
leegeunhyeok
0
180
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
680
Featured
See All Featured
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
480
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
350
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
980
BBQ
matthewcrist
90
10k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
4
4.4k
The Straight Up "How To Draw Better" Workshop
denniskardys
239
140k
Prompt Engineering for Job Search
mfonobong
0
590
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.7k
How Software Deployment tools have changed in the past 20 years
geshan
3
35k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1k
Code Reviewing Like a Champion
maltzj
529
40k