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
horimislime
August 17, 2016
Technology
1
1.4k
Swift Optional Extension Tips
horimislime
August 17, 2016
Tweet
Share
More Decks by horimislime
See All by horimislime
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
1.4k
How we build our app with minimum 3rd party dependencies
horimislime
0
77
サポート効率を上げるためのロギング環境構築
horimislime
7
3.7k
migrating-from-promise-to-reactive
horimislime
0
360
社内Swiftもくもく会成果発表
horimislime
0
110
ios-internationalization
horimislime
2
8.8k
UI testing in XCode7
horimislime
3
740
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.3k
Swift Libraryざっくり現状確認 / Swift library overview
horimislime
1
130
Other Decks in Technology
See All in Technology
より快適なエラーログ監視を目指して
leveragestech
4
1.3k
サーバー管理しないサーバーサービスManaged DevOps Pool
kkamegawa
0
110
eBPFのこれまでとこれから
yutarohayakawa
5
380
可視化により内部品質をあげるAIドキュメントリバース/20240910 Hiromitsu Akiba
shift_evolve
0
190
App Router を実プロダクトで採用して見えてきた勘所をちょっとだけ紹介
marokanatani
1
890
Segment Anything Model 2
tenten0727
3
620
SORACOMで実現するIoTのマルチクラウド対応 - IoTでのクリーンアーキテクチャの実現 -
kenichirokimura
0
360
contenteditableと向き合う
kikuchikakeru
2
290
サプライチェーン攻撃に備える
ryunen344
0
160
Autonomous Database Serverless 技術詳細 / adb-s_technical_detail_jp
oracle4engineer
PRO
15
40k
The XZ Backdoor Story
fr0gger
0
3.5k
LandingZoneAccelerator と学ぶ 「スケーラブルで安全なマルチアカウントAWS環境」と 私たちにもできるベストプラクティス
maimyyym
1
130
Featured
See All Featured
Designing for humans not robots
tammielis
248
25k
How to Think Like a Performance Engineer
csswizardry
16
950
Build The Right Thing And Hit Your Dates
maggiecrowley
30
2.3k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
166
48k
Documentation Writing (for coders)
carmenintech
65
4.3k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
41
6.5k
Designing Experiences People Love
moore
138
23k
GraphQLの誤解/rethinking-graphql
sonatard
65
9.8k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
190
16k
KATA
mclloyd
27
13k
Intergalactic Javascript Robots from Outer Space
tanoku
268
26k
Keith and Marios Guide to Fast Websites
keithpitt
408
22k
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