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.6k
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
320
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
1.9k
How we build our app with minimum 3rd party dependencies
horimislime
0
100
サポート効率を上げるためのロギング環境構築
horimislime
7
3.9k
migrating-from-promise-to-reactive
horimislime
0
400
社内Swiftもくもく会成果発表
horimislime
0
140
ios-internationalization
horimislime
2
8.9k
UI testing in XCode7
horimislime
3
820
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.4k
Other Decks in Technology
See All in Technology
Windows で省エネ
murachiakira
0
150
OCI Network Firewall 概要
oracle4engineer
PRO
1
7.7k
空間を設計する力を考える / 20251004 Naoki Takahashi
shift_evolve
PRO
3
250
GopherCon Tour 概略
logica0419
2
160
Why React!?? Next.jsそしてReactを改めてイチから選ぶ
ypresto
10
4.1k
神回のメカニズムと再現方法/Mechanisms and Playbook for Kamikai scrumat2025
moriyuya
4
300
Function calling機能をPLaMo2に実装するには / PFN LLMセミナー
pfn
PRO
0
820
Optuna DashboardにおけるPLaMo2連携機能の紹介 / PFN LLM セミナー
pfn
PRO
1
820
「技術負債にならない・間違えない」 権限管理の設計と実装
naro143
35
10k
Pythonによる契約プログラミング入門 / PyCon JP 2025
7pairs
5
2.4k
BirdCLEF+2025 Noir 5位解法紹介
myso
0
180
AIAgentの限界を超え、 現場を動かすWorkflowAgentの設計と実践
miyatakoji
0
110
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
36
2.5k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
33
2.4k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Unsuck your backbone
ammeep
671
58k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Raft: Consensus for Rubyists
vanstee
139
7.1k
Facilitating Awesome Meetings
lara
56
6.6k
Code Reviewing Like a Champion
maltzj
525
40k
Into the Great Unknown - MozCon
thekraken
40
2.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
140
34k
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