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
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
350
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
2k
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
840
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.5k
Other Decks in Technology
See All in Technology
ビジュアルプログラミングIoTLT vol.22
1ftseabass
PRO
0
140
KubeCon + CloudNativeCon NA ‘25 Recap, Extensibility: Gateway API / NRI
ladicle
0
130
Exadata Database Service ソフトウェアのアップデートとアップグレードの概要
oracle4engineer
PRO
1
1.2k
現場で活かす生成AI実践セミナー「広報×AI活用」編
matyuda
0
100
Kaggleコンペティション「MABe Challenge - Social Action Recognition in Mice」振り返り
yu4u
1
760
ReproでのicebergのStreaming Writeの検証と実運用にむけた取り組み
joker1007
0
450
ドキュメントからはじめる未来のソフトウェア
pkshadeck
3
1.2k
習慣とAIと環境 — 技術探求を続ける3つの鍵
azukiazusa1
3
780
書籍執筆での生成AIの活用
sat
PRO
1
210
AI時代のPMに求められるのは 「Ops」と「Enablement」
shimotaroo
1
330
Web Intelligence and Visual Media Analytics
weblyzard
PRO
1
6.8k
BPaaSオペレーション・kubell社内 n8n活用による効率化検証事例紹介
kubell_hr
0
270
Featured
See All Featured
The agentic SEO stack - context over prompts
schlessera
0
600
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
16th Malabo Montpellier Forum Presentation
akademiya2063
PRO
0
43
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.3k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
120
The SEO identity crisis: Don't let AI make you average
varn
0
57
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
61
49k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
220
Technical Leadership for Architectural Decision Making
baasie
1
220
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
0
130
4 Signs Your Business is Dying
shpigford
187
22k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
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