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
290
Any型をprotocolにキャストする時に 気をつけた方がいい話
PKPK-Carnage
May 21, 2019
Tweet
Share
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
460
アプリへの導線の増やし方.pdf
pkpkcarnage
0
710
「シェア機能」について考えてみた
pkpkcarnage
0
110
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
220
今日から使える! Optionalをいい感じに扱うtips
pkpkcarnage
0
1.3k
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
330
iOSの通信処理を簡潔に書く
pkpkcarnage
0
300
Other Decks in Programming
See All in Programming
オープンソースソフトウェアへの解像度🔬
utam0k
15
2.7k
overlayPreferenceValue で実現する ピュア SwiftUI な AdMob ネイティブ広告
uhucream
0
180
SpecKitでどこまでできる? コストはどれくらい?
leveragestech
0
720
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
680
私はどうやって技術力を上げたのか
yusukebe
43
18k
あなたとKaigi on Rails / Kaigi on Rails + You
shimoju
0
130
開発生産性を上げるための生成AI活用術
starfish719
3
580
Pull-Requestの内容を1クリックで動作確認可能にするワークフロー
natmark
2
500
CSC509 Lecture 05
javiergs
PRO
0
300
TFLintカスタムプラグインで始める Terraformコード品質管理
bells17
2
160
CSC509 Lecture 06
javiergs
PRO
0
260
2分台で1500examples完走!爆速CIを支える環境構築術 - Kaigi on Rails 2025
falcon8823
3
3.6k
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
232
18k
The Language of Interfaces
destraynor
162
25k
Fireside Chat
paigeccino
40
3.7k
Optimising Largest Contentful Paint
csswizardry
37
3.4k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
53k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
189
55k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
YesSQL, Process and Tooling at Scale
rocio
173
14k
GitHub's CSS Performance
jonrohan
1032
470k
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