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.2k
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
63
サポート効率を上げるためのロギング環境構築
horimislime
7
3.6k
migrating-from-promise-to-reactive
horimislime
0
350
社内Swiftもくもく会成果発表
horimislime
0
110
ios-internationalization
horimislime
2
8.7k
UI testing in XCode7
horimislime
3
700
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.2k
Swift Libraryざっくり現状確認 / Swift library overview
horimislime
1
120
1時間で作るSwiftアプリ / Swift Application in an hour
horimislime
2
110
Other Decks in Technology
See All in Technology
SSMエージェントはIAMロールの夢を見るか/ Do SSM Agents Dream Of IAM Roles?
yukihirochiba
0
1.4k
10年モノのレガシーPHPアプリケーションを移植しきるまでの泥臭くも長い軌跡 / legacy-php-app-migration
toshimaru
0
710
layerx-0-to-1-product-development-in-compound-startups
shnjtk
1
520
新卒1年目がプロジェクトを進めるときにコケたポイント
ryunakayama
1
110
Uniting Rust Servers and Clients through OpenAPI
ahl
0
430
「わたしたちのコード」を安定させるためにフレームワークとの距離を保つ / phperkaigi2024
blue_goheimochi
5
810
Datadog による 自己完結的アプリケーションモニタリング
recruitengineers
PRO
3
130
プロダクト開発ゼロイチの分類とロジックス事業がイチに至るまで
niwatakeru
0
100
[2024년 3월 세미나] 다른 회사는 퍼널 분석 어떻게 하고 있을까?
datarian
1
1.6k
How to Build a Strong Engineering Culture
alperhankendi
0
120
JaSST_Tokyo経験発表_20240313.pdf
ktanizaki
0
200
理想の組織も自分たちで作ろう! ―LayerXの「全員採用」を支える文化 / How to create our own ideal team
ar_tama
6
2.2k
Featured
See All Featured
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
226
16k
Stop Working from a Prison Cell
hatefulcrawdad
265
19k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
4
1.4k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
6
930
XXLCSS - How to scale CSS and keep your sanity
sugarenia
240
1.2M
YesSQL, Process and Tooling at Scale
rocio
160
13k
Music & Morning Musume
bryan
39
5.4k
VelocityConf: Rendering Performance Case Studies
addyosmani
319
23k
How GitHub (no longer) Works
holman
301
140k
ParisWeb 2013: Learning to Love: Crash Course in Emotional UX Design
dotmariusz
101
6.6k
Unsuck your backbone
ammeep
660
56k
Building a Modern Day E-commerce SEO Strategy
aleyda
15
6.3k
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