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のオプショナル
Search
Sato Takeshi
February 28, 2016
Technology
0
630
これでよくわかる!Swiftのオプショナル
iOS開発会議 in beez渋谷 47回目に発表した資料です。
Sato Takeshi
February 28, 2016
Tweet
Share
More Decks by Sato Takeshi
See All by Sato Takeshi
Swift愛好会 の 思い出
satotakeshi
0
62
Xcode 15, Swift 5.9で変わる開発体験
satotakeshi
3
2.8k
Meet passkeys
satotakeshi
2
320
What's new in Vision
satotakeshi
0
1.3k
Swift Concurrency入門
satotakeshi
10
4.8k
複数端末のつらさを乗り越えてiOS UITestを実行
satotakeshi
1
360
Xcodegenを個人アプリに導入
satotakeshi
3
700
SwiftUIで作る開閉式メニュー
satotakeshi
2
2.9k
swift-snapshot-testingでVisual Testingを効率化
satotakeshi
0
1.1k
Other Decks in Technology
See All in Technology
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
270
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
310
SREが投資するAIOps ~ペアーズにおけるLLM for Developerへの取り組み~
takumiogawa
2
480
Storybook との上手な向き合い方を考える
re_taro
5
860
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
200
Zennのパフォーマンスモニタリングでやっていること
ryosukeigarashi
0
230
マルチモーダル / AI Agent / LLMOps 3つの技術トレンドで理解するLLMの今後の展望
hirosatogamo
37
13k
SSMRunbook作成の勘所_20241120
koichiotomo
3
170
Lambda10周年!Lambdaは何をもたらしたか
smt7174
2
130
Flutterによる 効率的なAndroid・iOS・Webアプリケーション開発の事例
recruitengineers
PRO
0
120
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
190
CysharpのOSS群から見るModern C#の現在地
neuecc
2
3.6k
Featured
See All Featured
Navigating Team Friction
lara
183
14k
How to train your dragon (web standard)
notwaldorf
88
5.7k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Rails Girls Zürich Keynote
gr2m
94
13k
How to Ace a Technical Interview
jacobian
276
23k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
380
Docker and Python
trallard
40
3.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
720
Transcript
ʲே׆ʳ J04։ൃձٞ JOCFF[ौ୩ ճ ݄ ࠤ౻߶࢜
ࣗݾհ ʲ໊લʳ ࠤ౻߶࢜ ʲݞॻʳ ίϯηϓτͮ͘Γ͔Β૬ஊͰ͖ΔΤϯδχΞɻ ʲ͓ࣄʳ ࡢ݄ʹಠཱɻ લडୗ*5اۀͰΠϯϑϥͷอकӡ༻ۀ ࠓ༗ݶձࣾΧΠΧΠͰJ04ΞϓϦ࡞ ʲͰ͖Δ͜ͱʳ
SVCZ J04 BQBUDI MJOVY αʔόʔपΓʣ 5XJMJP ʲϦϦʔεΞϓϦʳ :VNF)PTIJ
4XJGUΛ։ൃͯͯ͠ 9DPEF͕ Λ ͔ͬͯʹೖΕͯ͘ΕΔ
9DPEFͷ͞ΕΔ͕··ʹ ͢ͷ͕ؾ࣋ͪѱ͍
4XJGUͷΦϓγϣφϧܕ Λཧͯ͠ ͳΜͱͳ͘ Λ͚ͭΔͷΛ Ί͍ͨ ɾТɾʆ
0CKFDU$Ͱͷ։ൃ ࣮ߦதͰͳ͍ͱ͔Βͳ͍όάΛ͏͔ͬΓ࡞ͬͪΌ͏ - (IBAction)crash:(UIButton *)sender { NSArray *array = [NSArray
array]; //Ϋϥογϡͤ͞Δ NSLog(@"%@", array[0]); }
ΞϓϦམͪͨʔ
ͦͷΤϥʔ 4XJGUͷΦϓγϣφϧܕͳΒ ࣄલʹݟ͚ͭΒΕΔ͔
4XJGUͷΦϓγϣφϧͰΨʔυ ੩తղੳͰΤϥʔʹͳΓͦ͏ͳͱ͜Ζ͕ผͰ͖Δ ˠજࡏతͳόάΛऔΓআ͖͍͢ʂ @IBAction func crash(sender: UIButton) { var array
: Array<String>? print(array?[0]) }
ͦΜͳ4XJGUͷΦϓγϣφϧܕ ͱͬͱྑ͘ͳΓ͍ͨ ɾ㱼ɾ
4XJGUͷΦϓγϣφϧܕ ·ͱΊ
0QUJPOBMܕͱʁ 0QUJPOBMܕOJMͷೖΛڐ͢ ඇPQUJPOBMܕOJMͷೖΛڐ͞ͳ͍ 0CKFDUJWF$ʹΦϒδΣΫτʹ͔͠OJMͳ͔ͬͨɻ ߏମɺ$ݴޠͷܕɺྻڍܕʹOJM͕ͳ͍ʣ TXJGUͲΜͳܕΦϓγϣφϧ͑Δ
0QUJPOBMܕએݴ var a: Int? var b: Optional<Int> // Int?ɹͱಉ͡ҙຯ enum
Optional<T> : Reflectable, NilLiteralConvertible { case None // nil ʹ૬͢Δ case Some(T) // T ܕʢૉͷܕʣͷ͕ೖΔʢInt ܕ String ܕͷͳͲʣ } 0QUJPOBM5FOVNܕͰఆٛ
0QUJPOBMܕએݴ͢ΔͱOJM͕ೖ͍͍ͬͯ var a: Int? a = nil //->nil͕ೖͬͯେৎ var b:
Int // ඇ optional ܕ b = nil // -> Nil cannot be assigned to type 'Int' ඇPQUJPOBMܕʹOJMΛೖ͠Α͏ͱ͢ΔͱɺίϯύΠϥΤϥʔ͕ൃੜ͢Δɻ 0QUJPOBMܕͷॳظOJM
0QUJPOBMܕͱඇ0QUJPOBMܕผͷܕ var unoptionalInt: Int = 1 print(unoptionalInt + 2) //
-> 3 var optionalInt : Int? = 1 print(optionalInt + 2) //Value of optional type 'Int?' not unwrapped; 0QUJPOBMܕΛ͏ʹΞϯϥοϓ͕ඞཁ
Ξϯϥοϓͷํ๏ w 'PSDFEVOXSBQQJOH w 0QUJPOBMDIBJOJOH w 0QUJPOBMCJOEJOH w ൺֱԋࢉࢠ w
*NQMJDJUMZ6OXSBQQFE0QUJPOBMܕͰએݴ͢Δ
'PSDFEVOXSBQQJOH var optionalInt : Int? = 1 print(optionalInt! + 2)
0QUJPOBMܕͷมͷޙΖʹʮʯΛ͚ͭΔ OJMͩͬͨΒϥϯλΠϜΤϥʔ
0QUJPOBMDIBJOJOH class Room { var adress : String = "ौ୩۠"
} class Person { var room : Room? } var aperson = Person() aperson.room?.adress = "ࠇ۠" aperson.room = Room() print(aperson.room?.adress) ʮ ʯΛͬͯΞϯϥοϓ͢Δ͜ͱ ΞϯϥοϓΛͨ͠ޙʹɺ࠷ऴతͳΓΛPQUJPOBMܕʹϥοϓͯ͠ ͔Βฦ͢ɻ
0QUJPOBM#JOEJOH if let adress = aperson.room?.adress{ print(adress) }else{ print("ॅॴ͕ొ͞Ε͍ͯ·ͤΜ") }
while let adress = aperson.room?.adress{ print(adress) break } JGXIJMFจͷ݅ࣜͰએݴ͞ΕɺPQUJPOBMܕͷมΛೖ͞Εͨ มɺඇPQUJPOBMܕʹͳΔɻ ͜ΕΛʮPQUJPOBMCJOEJOHʯͱݴ͏
ൺֱԋࢉࢠ var wrappedInt: Int? = 1 // Optional ܕ print(wrappedInt
== 1) // -> true print(wrappedInt >= 1) // -> true print(wrappedInt > 1) // -> false print(wrappedInt <= 1) // -> true print(wrappedInt < 1) // -> false print(wrappedInt != 1) // -> false wrappedInt = nil print(wrappedInt == 1) // -> false ൺֱԋࢉࢠΛ͏ͱɺPQUJPOBMܕͷม͕ࣗಈతʹΞϯϥοϓ͞Ε Δɻ OJM͕ೖͬͯΤϥʔʹͳΒͳ͍
*NQMJDJUMZ6OXSBQQFE0QUJPOBMܕͰએݴ /**implicitly unwrapped optional*/ var assumedString: String! = "An implicitly
unwrapped optional string." let implicitString: String = assumedString // no need for an exclamation mark” print(implicitString) /*ΦϓγϣφϧόΠϯσΟϯάͰ͖Δ*/ if let definiteString = assumedString { print(definiteString) } ࣗಈతʹΞϯϥοϓ͞ΕΔPQUJPOBMܕ ʮʯͰએݴ͢Δ
*NQMJDJUMZ6OXSBQQFE0QUJPOBMܕͰએݴ var implicityInt: Int! var implicityInt2: ImplicitlyUnwrappedOptional<Int> // Int! ͱಉ͡ҙຯ
ʮʯ*NQMJDJUMZ6OXSBQQFE0QUJPOBM5ͷγϯλοΫεγϡΨʔɻ *NQMJDJUMZ6OXSBQQFE0QUJPOBM5FOVNܕͰఆٛ͞Ε͍ͯΔ enum ImplicitlyUnwrappedOptional<T> : Reflectable, NilLiteralConvertible { case None // nil ʹ૬͢Δ case Some(T) // T ܕʢૉͷܕʣͷ͕ೖΔʢInt ܕ String ܕͷ ͳͲʣ }
·ͱΊ એݴ࣌ ࣮ߦ࣌ 0QUJPOBMܕ 0QUJPOBMDIBJOJOH *NQMJDJUMZ 6OXSBQQFE 0QUJPOBMܕ 'PSDFEVOXSBQQJOH
αϯϓϧίʔυ IUUQTHJTUHJUIVCDPN4BUP5BLFTIJ9FFCDGCFBDDF
ࢀߟ63- 5IF4XJGU1SPHSBNNJOH-BOHVBHF 4XJGU IUUQTJUVOFTBQQMFDPNKQCPPLTXJGUQSPHSBNNJOH MBOHVBHFJE NU OVMMࢀরͷߟҊԯυϧ୯Ґͷաͪʁ IUUQEFWFMPQFSTTSBEKQTUPSZOVMM &'&"&"&
&&"&"'&& &"#&%&#%%&"& &&&"&'#$'
ࢀߟ63- <4XJGU>4XJGUͷPQUJPOBMWBMVFͷศར͞ʮ ʯͱʮʯͰΑΓݎ࿚ͳ ίʔυ IUUQRJJUBDPNOPSJJUFNTBGBDDGGCED <4XJGU>0QUJPOBMܕʹ͍ͭͯͷ·ͱΊ7FS IUUQRJJUBDPNDPUSQFQFJUFNTDDBBGG 4XJGUͷΦϓγϣφϧνΣʔϯʢPQUJPOBMDIBJOJOHʣ IUUQCMPHLJUPLPDPNFOUSZ ΦϓγϣφϧܕɻʙͳΜͱͳ͚͘Δ
໓ IUUQXXXTMJEFTIBSFOFUIBTFHBXBUPNPLJTT