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
270
Any型をprotocolにキャストする時に 気をつけた方がいい話
PKPK-Carnage
May 21, 2019
Tweet
Share
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
430
アプリへの導線の増やし方.pdf
pkpkcarnage
0
680
「シェア機能」について考えてみた
pkpkcarnage
0
98
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
190
今日から使える! Optionalをいい感じに扱うtips
pkpkcarnage
0
1.2k
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
300
iOSの通信処理を簡潔に書く
pkpkcarnage
0
270
Other Decks in Programming
See All in Programming
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1.1k
php-conference-japan-2024
tasuku43
0
360
AWSのLambdaで PHPを動かす選択肢
rinchoku
2
300
競技プログラミングへのお誘い@阪大BOOSTセミナー
kotamanegi
0
360
暇に任せてProxmoxコンソール 作ってみました
karugamo
2
730
Online-Dokumentation, die hilft: Strukturen, Prozesse, Tools
ahus1
0
100
テストケースの名前はどうつけるべきか?
orgachem
PRO
0
160
PHPで学ぶプログラミングの教訓 / Lessons in Programming Learned through PHP
nrslib
4
390
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
190
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
150
【re:Growth 2024】 Aurora DSQL をちゃんと話します!
maroon1st
0
800
AppRouterを用いた大規模サービス開発におけるディレクトリ構成の変遷と問題点
eiganken
1
150
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
6
520
Fireside Chat
paigeccino
34
3.1k
Done Done
chrislema
182
16k
Become a Pro
speakerdeck
PRO
26
5k
How GitHub (no longer) Works
holman
311
140k
What's in a price? How to price your products and services
michaelherold
244
12k
Intergalactic Javascript Robots from Outer Space
tanoku
270
27k
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