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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
horimislime
August 17, 2016
Technology
1.8k
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
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
410
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
2.2k
How we build our app with minimum 3rd party dependencies
horimislime
0
120
サポート効率を上げるためのロギング環境構築
horimislime
7
4k
migrating-from-promise-to-reactive
horimislime
0
430
社内Swiftもくもく会成果発表
horimislime
0
170
ios-internationalization
horimislime
2
9.1k
UI testing in XCode7
horimislime
3
870
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.5k
Other Decks in Technology
See All in Technology
クラウドセキュリティ入門 ~安全なクラウド利用のための基礎知識~
lhazy
11
8.2k
グローバル基準のSREは、運用現場でどう機能したか:成熟度アセスメントの実践 / SRE NEXT 2026
sorawatanabe
0
130
初めてのGitHub Actions / GitHub Actions at First
tooppoo
0
150
AI駆動開発は個人技からチーム戦へ:組織でAIを使いこなすための実践設計
moongift
PRO
0
470
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
53k
20260804_Q4AzureUpdateBite_FabricDataAgentの精度を高める設計.pdf
matayuuu
1
110
名刺メーカーDevグループ 紹介資料
sansan33
PRO
0
1.2k
【CEDEC2026】専門性の高いデフォルメチームが挑んだ人材育成戦略 〜Cygames Academiaの企画から実施まで〜
cygames
PRO
0
520
Invisible to AI? Making TYPO3 Sites Quotable by AI Search Systems
wolfgangwagner
0
140
攻撃と防御で学ぶAI時代のプロダクトセキュリティ演習
recruitengineers
PRO
6
1.5k
SnowflakeCoCoでデータエンジニアリング!
foursue
0
180
え?フロントエンドエンジニアの ワイがインフラも!?
puku0x
1
390
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.5k
The B2B funnel & how to create a winning content strategy
katarinadahlin
PRO
1
460
Crafting Experiences
bethany
1
240
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
240
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
4.2k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
200
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
490
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
GitHub's CSS Performance
jonrohan
1033
470k
Mobile First: as difficult as doing things right
swwweet
225
10k
The untapped power of vector embeddings
frankvandijk
2
1.8k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.1k
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