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
Swift Optional Extension Tips
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
horimislime
August 17, 2016
Technology
1
1.7k
Swift Optional Extension Tips
horimislime
August 17, 2016
Tweet
Share
More Decks by horimislime
See All by horimislime
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
370
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
2.1k
How we build our app with minimum 3rd party dependencies
horimislime
0
110
サポート効率を上げるためのロギング環境構築
horimislime
7
3.9k
migrating-from-promise-to-reactive
horimislime
0
410
社内Swiftもくもく会成果発表
horimislime
0
150
ios-internationalization
horimislime
2
9k
UI testing in XCode7
horimislime
3
850
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.5k
Other Decks in Technology
See All in Technology
AI が Approve する開発フロー / How AI Reviewers Accelerate Our Development
zaimy
1
250
Introduction to Bill One Development Engineer
sansan33
PRO
0
380
Snowflake Night #2 LT
taromatsui_cccmkhd
0
300
Lookerの最新バージョンv26.2がやばい話
waiwai2111
1
150
LLM活用の壁を超える:リクルートR&Dの戦略と打ち手
recruitengineers
PRO
1
180
Webアクセシビリティ技術と実装の実際
tomokusaba
0
180
Devinを導入したら予想外の人たちに好評だった
tomuro
0
690
社内ワークショップで終わらせない 業務改善AIエージェント開発
lycorptech_jp
PRO
1
430
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
14k
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
10k
全自動で回せ!Claude Codeマーケットプレイス運用術
yukyu30
3
150
Windows ネットワークを再確認する
murachiakira
PRO
0
210
Featured
See All Featured
Become a Pro
speakerdeck
PRO
31
5.8k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
0
160
Measuring & Analyzing Core Web Vitals
bluesmoon
9
770
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
3.9k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
110
Odyssey Design
rkendrick25
PRO
2
530
The Mindset for Success: Future Career Progression
greggifford
PRO
0
260
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
140
GitHub's CSS Performance
jonrohan
1032
470k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Unsuck your backbone
ammeep
672
58k
Transcript
Optional Extension Tips Kyobashi.swift #2 @horimislime
About • ! @horimislime • " ҿ৯ళ͚iPadΞϓϦʮτϨλʯ • ❤ ιϑτΣΞઃܭͱ͔ӡ༻ͱ͔
OptionalͷຯͳπϥΈ
Objective-C if (users.count == 0) { // nil͔ۭͩͬͨΒԿ͔͢Δ } if
(name.length == 0) { // ໊લ͕nil͔ۭͩͬͨΒԿ͔͢Δ }
Swift // Compile error ! if users?.isEmpty { // ...
} if name?.isEmpty { // ... }
OptionalΛѻ͏ࡍͷ͓ଋ guard let users = users where !users.isEmpty else {
// nil͔ۭͩͬͨΒԿ͔͢Δ }
Nil Coalescing • ͘ॻ͚Δ͕ɺͺͬͱݟͷՄಡੑʹ͚ܽΔ if array?.isEmpty ?? false { //
... } if !(str ?? "").isEmpty { // ... }
θϩఆ • nilͷൺֱৗʹfalse • Objective-C→Swiftʹࣸܦ͢Δͱ௧͍ʹ // Objective-C if (str.count ==
0) { ... } /// Swift if str?.characters.count == 0 { ... }
• ܕ͕ݫີʹͳͬͨͷͷɺʮۭʯͷѻ͍͕໘ • nilͱۭͷঢ়ଶΛಉ͡Α͏ʹѻ͏ʹʁ
Optional Extension • isNilOrEmptyతͳͷ • Optional<String>ʹextensionੜͤͳ͍ // Error: 'Wrapped' constrained
to non-protocol type 'String' extension Optional where Wrapped: String { var isNilOrEmpty: Bool { return self?.isEmpty ?? true } }
Optional Extension • ಠࣗprotocolΛ༻ҙ͠ɺStringʹ४ڌͤ͞Δ protocol StringType { var isEmpty: Bool
{ get } } extension String: StringType {} extension Optional where Wrapped: StringType { var isNilOrEmpty: Bool { return self?.isEmpty ?? true } }
/// Before if name == nil || name.isEmpty else {
// nil͔ۭͩͬͨΒԿ͔͢Δ } /// After if name.isNilOrEmpty { ... }
·ͱΊ • nilͱۭҰॹͰߟ͑Δ߹͕ଟ͍ • ຯͳͱ͜ΖͰՄಡੑΛམͱ͞ͳ͍Ұ • Objective-C͔ΒSwiftҠߦظʹ͋Δͱศར͔
None