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
250
Any型をprotocolにキャストする時に 気をつけた方がいい話
PKPK-Carnage
May 21, 2019
Tweet
Share
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
380
アプリへの導線の増やし方.pdf
pkpkcarnage
0
610
「シェア機能」について考えてみた
pkpkcarnage
0
88
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
170
今日から使える! Optionalをいい感じに扱うtips
pkpkcarnage
0
1.1k
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
260
iOSの通信処理を簡潔に書く
pkpkcarnage
0
240
Other Decks in Programming
See All in Programming
二郎系ラーメンのコールで学ぶ AST 解析
memory1994
PRO
7
1.7k
はてなにおける CSS Modules、及び CSS Modules に足りないもの / CSS Modules in Hatena, and CSS Modules missing parts
mizdra
7
920
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
140
DMMプラットフォームがTiDB Cloudを採用した背景
pospome
8
4k
SIMD Parallel Programming with the Vector API
josepaumard
0
160
Komplexe Oberflächen mit SVG und der Web Animation API
joergneumann
0
670
Netty Chicago Java User Group 2024-04-17
sullis
0
170
検証も兼ねて個人開発でHonoとかと向き合った話
hanetsuki
0
890
OpenAPIを中心に考えるAPI開発入門 / Introduction to API Development with a Focus on OpenAPI
seike460
PRO
2
170
Git Rebase
bkuhlmann
11
1.6k
CA.swift19 恋するAIアプリ開発の裏側
oskmr
0
360
educure_カリキュラム生操作マニュアル.pdf
linew_official
0
760
Featured
See All Featured
Agile that works and the tools we love
rasmusluckow
325
20k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
2
3.4k
Code Review Best Practice
trishagee
55
15k
Adopting Sorbet at Scale
ufuk
68
8.6k
WebSockets: Embracing the real-time Web
robhawkes
59
7k
Web development in the modern age
philhawksworth
202
10k
Design by the Numbers
sachag
274
18k
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
187
16k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
34
8.9k
Designing for humans not robots
tammielis
248
25k
Intergalactic Javascript Robots from Outer Space
tanoku
266
26k
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