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
Any型をprotocolにキャストする時に 気をつけた方がいい話
Search
PKPK-Carnage
May 21, 2019
Programming
0
280
Any型をprotocolにキャストする時に 気をつけた方がいい話
PKPK-Carnage
May 21, 2019
Tweet
Share
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
450
アプリへの導線の増やし方.pdf
pkpkcarnage
0
690
「シェア機能」について考えてみた
pkpkcarnage
0
100
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
210
今日から使える! Optionalをいい感じに扱うtips
pkpkcarnage
0
1.2k
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
320
iOSの通信処理を簡潔に書く
pkpkcarnage
0
290
Other Decks in Programming
See All in Programming
ComposeでWebアプリを作る技術
tbsten
0
130
AIコーディングの本質は“コード“ではなく“構造“だった / The essence of AI coding is not “code” but "structure
seike460
PRO
1
230
知識0からカンファレンスやってみたらこうなった!
syossan27
4
130
データベースの技術選定を突き詰める ~複数事例から考える最適なデータベースの選び方~
nnaka2992
0
210
AWS Summit Hong Kong 2025: Reinventing Programming - How AI Transforms Our Enterprise Coding Approach
dwchiang
0
140
On-the-fly Suggestions of Rewriting Method Deprecations
ohbarye
3
5.3k
ComposeでのPicture in Picture
takathemax
0
130
AIコーディングの理想と現実
tomohisa
37
39k
Носок на сок
bo0om
0
1.3k
カオスに立ち向かう小規模チームの装備の選択〜フルスタックTSという装備の強み _ 弱み〜/Choosing equipment for a small team facing chaos ~ Strengths and weaknesses of full-stack TS~
bitkey
1
140
eBPF超入門「o11yに使える」とは (20250424_eBPF_o11y)
thousanda
1
120
iOSアプリで測る!名古屋駅までの 方向と距離
ryunakayama
0
160
Featured
See All Featured
BBQ
matthewcrist
88
9.6k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.4k
Building Better People: How to give real-time feedback that sticks.
wjessup
368
19k
Build The Right Thing And Hit Your Dates
maggiecrowley
35
2.7k
Producing Creativity
orderedlist
PRO
344
40k
Fontdeck: Realign not Redesign
paulrobertlloyd
84
5.5k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
Agile that works and the tools we love
rasmusluckow
329
21k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.2k
Code Review Best Practice
trishagee
68
18k
Fireside Chat
paigeccino
37
3.4k
Six Lessons from altMBA
skipperchong
28
3.8k
Transcript
AnyܕΛprotocolʹΩϟετ͢Δ࣌ʹ ؾΛ͚ͭͨํ͕͍͍ 2019/5/21 ͋Δ͋ΔLT vol4 PKPK-Carnage(Tomosuke Okada) 1
About me • iOSྺ2ͪΐͬͱ • Qiita → @PKPK-Carnage • Github
→ PKPK-Carnage • Twitter → @PKPK-Carnage
ͦΜͳͷ؆୯͡ΌΜ 3
Any as? Protocol 4
ྫɿNotification 5 protocol Animal {} struct Dog: Animal {}
ྫɿNotification 6 class HogeClass { func postNotification() { // DogΛૹΔ
NotificationCenter.default.post( name: .init("Animal"), object: Dog() ) } }
ྫɿNotification 7 class FugaClass { init() { NotificationCenter.default.addObserver( self, selector:
#selector(self.didReceivedNotification(_:)), name: .init("Animal"), object: nil ) } @objc func didReceivedNotification(_ notification: Notification) { } }
ྫɿNotification 8 @objc func didReceivedNotification(_ notification: Notification) { // notification.objectͷதΛग़ྗ͢Δ
print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷதΛग़ྗ͢Δ let animal = notification.object as? Animal print("\(String(describing: animal))") } Optional(__lldb_expr_11.Dog()) nil
9
SR-3871 10
SR-3871 • Swiftͷstructʹas AnyObjectΛߦͬͯɺAnyܕʹ֨ೲͨ࣌͠ʹɺ Protocolͷ४ڌఆ͕ߦ͑ͳ͘ͳΔόά͕͋Δ • AnyܕͷΦϒδΣΫτʹas AnyObjectΛߦ͑ɺͪΌΜͱProtocol ͷ४ڌఆ͕ߦ͑ΔΑ͏ʹͳΔɻ 11
࣮ࡍʹͬͯΈͨ 12 @objc func didReceivedNotification(_ notification: Notification) { // notification.objectͷதΛग़ྗ͢Δ
print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷதΛग़ྗ͢Δ let animal = notification.object as? Animal print("\(String(describing: animal))") }
࣮ࡍʹͬͯΈͨ 13 @objc func didReceivedNotification(_ notification: Notification) { // notification.objectͷதΛग़ྗ͢Δ
print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷதΛग़ྗ͢Δ let animal = (notification.object as AnyObject) as? Animal print("\(String(describing: animal))") }
࣮ࡍʹͬͯΈͨ 14 Optional(__lldb_expr_11.Dog()) Optional(__lldb_expr_11.Dog()) @objc func didReceivedNotification(_ notification: Notification) {
// notification.objectͷதΛग़ྗ͢Δ print("\(String(describing: notification.object))") // notification.objectΛAnimal(Protocol)ʹΩϟετͨ͠ޙͷதΛग़ྗ͢Δ let animal = (notification.object as AnyObject) as? Animal print("\(String(describing: animal))") }
15
Qiitaॻ͖·ͨ͠ 16 https://qiita.com/PKPK-Carnage/items/5e2eda7735aa24f807a9
͓ΘΓ 17