Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Swift Optional Extension Tips
horimislime
August 17, 2016
Technology
1
980
Swift Optional Extension Tips
horimislime
August 17, 2016
Tweet
Share
More Decks by horimislime
See All by horimislime
How we build our app with minimum 3rd party dependencies
horimislime
0
41
サポート効率を上げるためのロギング環境構築
horimislime
7
3.3k
migrating-from-promise-to-reactive
horimislime
0
290
社内Swiftもくもく会成果発表
horimislime
0
96
ios-internationalization
horimislime
2
8.3k
UI testing in XCode7
horimislime
3
660
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2k
Swift Libraryざっくり現状確認 / Swift library overview
horimislime
1
110
1時間で作るSwiftアプリ / Swift Application in an hour
horimislime
2
100
Other Decks in Technology
See All in Technology
開発環境のセキュリティおよびCI/CDパイプラインのセキュア化
rung
PRO
13
5.4k
PMMやプロダクト関係者と協働するために役割を整理した話 / 20220810_pdmtipslt
rakus_dev
0
130
[Journal club] Vision Transformer with Deformable Attention
keio_smilab
PRO
0
120
ぼくらが選んだ次のMySQL 8.0 / MySQL80 Which We Choose
line_developers
PRO
7
3.4k
増田亨さんによる 「設計の考え方とやり方」勉強会オープニング
tsuyok
0
240
質の良い”カイゼン”の為の質の良い「振り返り」
shirayanagiryuji
0
140
DeFiChain Tech Talk - DFI Uniswap Staking, DeFi Options & DeFi Meta Chain
uzyn
0
130
Power BI のうらがわ
hanaseleb
1
170
Istioを活用したセキュアなマイクロサービスの実現/Secure Microservices with Istio
ido_kara_deru
3
460
ふりかえりの技術 / retrospectives
soudai
3
190
殺虫剤のパラドックスの真実 / The Truth of The Pesticide Paradox
kzsuzuki
1
230
Settlement simulation testing to ensure correct settlement processing
applepine1125
2
1.6k
Featured
See All Featured
The Invisible Customer
myddelton
110
11k
Documentation Writing (for coders)
carmenintech
48
2.6k
Why Our Code Smells
bkeepers
PRO
324
55k
How GitHub Uses GitHub to Build GitHub
holman
465
280k
Large-scale JavaScript Application Architecture
addyosmani
499
110k
Making Projects Easy
brettharned
99
4.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
107
16k
Testing 201, or: Great Expectations
jmmastey
21
5.5k
Intergalactic Javascript Robots from Outer Space
tanoku
260
25k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
21
1.4k
Done Done
chrislema
174
14k
How to Ace a Technical Interview
jacobian
267
21k
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