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
400
スタートアップの急成長に寄り添う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
420
社内Swiftもくもく会成果発表
horimislime
0
160
ios-internationalization
horimislime
2
9k
UI testing in XCode7
horimislime
3
870
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.5k
Other Decks in Technology
See All in Technology
AWS Security Hub CSPMの成功・失敗体験
cmusudakeisuke
0
300
Android の公式 Skill / Android skills
yanzm
0
160
SONiCの統計情報を取得したい
sonic
0
240
自分が詳しくない領域でAIを使う #プロヒス2026
konifar
18
6k
Chainlitで作るお手軽チャットUI
ynt0485
0
280
マルチアカウント環境での コーディングエージェントを使った障害調査が大変なので AIエージェントにReadOnly権限を付与してみた / ReadOnly AI Agents for Multi-Account AWS Incident Response
yamaguchitk333
2
110
フィジカル版Github Onshapeの紹介
shiba_8ro
0
290
SteampipeとExcel Power QueryでAWS構成定義書の作成を自動化する
jhashimoto
0
160
When Platform Engineering Meets GenAI
sucitw
0
130
「勝手に広まる」人気 AI エージェントを爆速で作ろう!(AWS Summit Japan 2026講演資料)
minorun365
PRO
10
2k
あなたの知らないPDFのアクセシビリティ
lycorptech_jp
PRO
0
220
Claude Codeをどのように キャッチアップしているか
oikon48
13
8.6k
Featured
See All Featured
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Believing is Seeing
oripsolob
1
150
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.5k
WCS-LA-2024
lcolladotor
0
650
Accessibility Awareness
sabderemane
1
140
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
160
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
Context Engineering - Making Every Token Count
addyosmani
9
970
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
630
Building a Scalable Design System with Sketch
lauravandoore
463
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