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.6k
Swift Optional Extension Tips
horimislime
August 17, 2016
Tweet
Share
More Decks by horimislime
See All by horimislime
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
280
スタートアップの急成長に寄り添うOn-Call体制構築とその変遷
horimislime
3
1.8k
How we build our app with minimum 3rd party dependencies
horimislime
0
96
サポート効率を上げるためのロギング環境構築
horimislime
7
3.9k
migrating-from-promise-to-reactive
horimislime
0
400
社内Swiftもくもく会成果発表
horimislime
0
140
ios-internationalization
horimislime
2
8.9k
UI testing in XCode7
horimislime
3
800
UIテストをカジュアルに自動化 / UI Automation using Remote
horimislime
2
2.4k
Other Decks in Technology
See All in Technology
PHP開発者のためのSOLID原則再入門 #phpcon / PHP Conference Japan 2025
shogogg
4
740
Node-REDのFunctionノードでMCPサーバーの実装を試してみた / Node-RED × MCP 勉強会 vol.1
you
PRO
0
110
“社内”だけで完結していた私が、AWS Community Builder になるまで
nagisa53
1
390
【5分でわかる】セーフィー エンジニア向け会社紹介
safie_recruit
0
26k
【TiDB GAME DAY 2025】Shadowverse: Worlds Beyond にみる TiDB 活用術
cygames
0
1.1k
AIのAIによるAIのための出力評価と改善
chocoyama
2
550
生成AI時代 文字コードを学ぶ意義を見出せるか?
hrsued
1
420
Github Copilot エージェントモードで試してみた
ochtum
0
100
Amazon ECS & AWS Fargate 運用アーキテクチャ2025 / Amazon ECS and AWS Fargate Ops Architecture 2025
iselegant
16
5.5k
20250625 Snowflake Summit 2025活用事例 レポート / Nowcast Snowflake Summit 2025 Case Study Report
kkuv
1
310
Witchcraft for Memory
pocke
1
330
AIエージェント最前線! Amazon Bedrock、Amazon Q、そしてMCPを使いこなそう
minorun365
PRO
15
5.1k
Featured
See All Featured
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
32
2.3k
How to train your dragon (web standard)
notwaldorf
93
6.1k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Producing Creativity
orderedlist
PRO
346
40k
RailsConf 2023
tenderlove
30
1.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Git: the NoSQL Database
bkeepers
PRO
430
65k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
50k
Designing for humans not robots
tammielis
253
25k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Being A Developer After 40
akosma
90
590k
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