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.8k
1
Share
Swift Optional Extension Tips
horimislime
August 17, 2016
More Decks by horimislime
See All by horimislime
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
390
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
2.2k
How we build our app with minimum 3rd party dependencies
horimislime
0
110
サポート効率を上げるためのロギング環境構築
horimislime
7
4k
migrating-from-promise-to-reactive
horimislime
0
420
社内Swiftもくもく会成果発表
horimislime
0
160
ios-internationalization
horimislime
2
9k
UI testing in XCode7
horimislime
3
860
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.5k
Other Decks in Technology
See All in Technology
Databricks 月刊サービスアップデート 2026年05月号
tyosi1212
0
120
Oracle AI Database@Azure:サービス概要のご紹介
oracle4engineer
PRO
6
1.8k
Cloud Run のアップデート 触ってみる&紹介
gre212
0
270
最低限これだけ押さえれ大丈夫_Claude Enterprise/Team企業展開ガバナンス入門
tkikuchi
1
570
関西に縁あるMicrosoft MVPsが語るCopilotの未来
kasada
0
750
Claude Codeを組織で使いこなす— サーバサイドAIエージェント運用の実践知
techtekt
PRO
0
140
自称宇宙最速で不合格となったAIP-C01にリベンジを果たすべくAIで問題集アプリを作ってみた。
yama3133
0
250
Ruby::Boxでできること、Refinementsでできること
joker1007
2
110
Diagnosing performance problems without the guesswork
elenatanasoiu
0
130
速さだけじゃない! VoidZero ツールが移行先に選ばれる理由
mizdra
PRO
6
700
個人AIからチームAIへ:開発における品質と生産性の再設計
moongift
PRO
0
320
OpenClawとHermesAgentでAI新入社員を作った話
takanoriyanada
0
150
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
Producing Creativity
orderedlist
PRO
348
40k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
760
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
70
39k
The untapped power of vector embeddings
frankvandijk
2
1.7k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Ethics towards AI in product and experience design
skipperchong
2
290
How GitHub (no longer) Works
holman
316
150k
Are puppies a ranking factor?
jonoalderson
1
3.4k
Building an army of robots
kneath
306
46k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
190
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
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