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.3k
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
73
サポート効率を上げるためのロギング環境構築
horimislime
7
3.7k
migrating-from-promise-to-reactive
horimislime
0
350
社内Swiftもくもく会成果発表
horimislime
0
110
ios-internationalization
horimislime
2
8.7k
UI testing in XCode7
horimislime
3
730
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.3k
Swift Libraryざっくり現状確認 / Swift library overview
horimislime
1
130
1時間で作るSwiftアプリ / Swift Application in an hour
horimislime
2
120
Other Decks in Technology
See All in Technology
初中級者用如何使用backlog -VALE TUDOEDITION-
in0u
0
140
What is DRE? - Road to SRE NEXT@広島
chanyou0311
3
630
VPoEの視点から見た、ヘンリーがサーバーサイドKotlinを使う理由 / Why Server-side Kotlin 2024
cho0o0
1
420
AutomatedLabを使って内部ペンテストを勉強しよう! -やられ社内ネットワークの自動構築-
n_etupirka
1
610
Classmethod Odyssey 登壇資料
yamahiro
0
390
サービス開発を前に進めるために 新米リードエンジニアが 取り組んだこと / Steps Taken by a Novice Lead Engineer to Advance Service Development
nologyance
0
180
フルリモートワークはエンジニアの夢を叶えたか? #cm_odyssey
mamohacy
2
600
データ分析基盤を作ってみよう~設計編~
nrinetcom
PRO
1
110
OSSコミットしてZennの課題を解決した話
dyoshikawa1993
0
150
CEL(Common Expression Language)で書いた条件にマッチしたIAM Policyを見つける / iam-policy-finder
fujiwara3
0
720
推薦システムを本番導入する上で一番優先すべきだったこと~NewsPicks記事推薦機能の改善事例を元に~
morinota
0
130
LINE WORKSへ簡単通知!Incoming Webhookアプリの紹介
mmclsntr
0
110
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
325
20k
Thoughts on Productivity
jonyablonski
64
4.1k
Optimizing for Happiness
mojombo
373
69k
Fantastic passwords and where to find them - at NoRuKo
philnash
42
2.7k
Clear Off the Table
cherdarchuk
89
320k
A Philosophy of Restraint
colly
200
16k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
360
22k
RailsConf 2023
tenderlove
16
720
How GitHub Uses GitHub to Build GitHub
holman
471
290k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
26
1.6k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
44
4.7k
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