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
新職業『オーケストレーター』誕生 — エージェント10体を同時に回すAgentOps
gunta
4
1.3k
Master Dataグループ紹介資料
sansan33
PRO
1
4.4k
Devinを導入したら予想外の人たちに好評だった
tomuro
0
880
DX Improvement at Scale
ntk1000
2
280
Claude Cowork Plugins を読む - Skills駆動型業務エージェント設計の実像と構造
knishioka
0
260
ソフトウェアアーキテクトのための意思決定術: Create Decision Readiness—The Real Skill Behind Architectural Decision
snoozer05
PRO
29
9k
組織のSREを推進するためのPlatform EngineeringとEKS / Platform Engineering and EKS to drive SRE in your organization
chmikata
0
180
研究開発部メンバーの働き⽅ / Sansan R&D Profile
sansan33
PRO
4
22k
Databricksアシスタントが自分で考えて動く時代に! エージェントモード体験もくもく会
taka_aki
0
320
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
15
95k
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
14k
製造業ドメインにおける LLMプロダクト構築: 複雑な文脈へのアプローチ
caddi_eng
1
450
Featured
See All Featured
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.3k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
170
ラッコキーワード サービス紹介資料
rakko
1
2.5M
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
600
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
110
The Limits of Empathy - UXLibs8
cassininazir
1
240
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Design in an AI World
tapps
0
160
First, design no harm
axbom
PRO
2
1.1k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
470
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
490
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
310
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