Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
340
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
2k
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
9k
UI testing in XCode7
horimislime
3
830
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.4k
Other Decks in Technology
See All in Technology
生成AIでテスト設計はどこまでできる? 「テスト粒度」を操るテーラリング術
shota_kusaba
0
530
re:Invent2025 コンテナ系アップデート振り返り(+CloudWatchログのアップデート紹介)
masukawa
0
320
AWS re:Invent 2025で見たGrafana最新機能の紹介
hamadakoji
0
130
SSO方式とJumpアカウント方式の比較と設計方針
yuobayashi
7
510
Playwright x GitHub Actionsで実現する「レビューしやすい」E2Eテストレポート
kinosuke01
0
410
Security Diaries of an Open Source IAM
ahus1
0
130
[デモです] NotebookLM で作ったスライドの例
kongmingstrap
0
100
Haskell を武器にして挑む競技プログラミング ─ 操作的思考から意味モデル思考へ
naoya
4
840
Edge AI Performance on Zephyr Pico vs. Pico 2
iotengineer22
0
110
Lambdaの常識はどう変わる?!re:Invent 2025 before after
iwatatomoya
1
340
コミューンのデータ分析AIエージェント「Community Sage」の紹介
fufufukakaka
0
440
直接メモリアクセス
koba789
0
280
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.3k
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
Leading Effective Engineering Teams in the AI Era
addyosmani
8
1.3k
Designing Experiences People Love
moore
143
24k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
GitHub's CSS Performance
jonrohan
1032
470k
BBQ
matthewcrist
89
9.9k
Optimising Largest Contentful Paint
csswizardry
37
3.5k
For a Future-Friendly Web
brad_frost
180
10k
jQuery: Nuts, Bolts and Bling
dougneiner
65
8.2k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
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