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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
PKPK-Carnage
May 21, 2019
Programming
310
0
Share
Any型をprotocolにキャストする時に 気をつけた方がいい話
PKPK-Carnage
May 21, 2019
More Decks by PKPK-Carnage
See All by PKPK-Carnage
iOSアプリを堅牢にデザインするために知っておくべきたった1つのこと
pkpkcarnage
0
510
アプリへの導線の増やし方.pdf
pkpkcarnage
0
750
「シェア機能」について考えてみた
pkpkcarnage
0
120
リリースビルドでのみ起こる謎のバグに見舞われた話
pkpkcarnage
0
230
今日から使える! Optionalをいい感じに扱うtips
pkpkcarnage
0
1.3k
もっと早く教えて欲しかった画面遷移
pkpkcarnage
0
360
iOSの通信処理を簡潔に書く
pkpkcarnage
0
320
Other Decks in Programming
See All in Programming
Sans tests, vos agents ne sont pas fiables
nabondance
0
140
密結合なバックエンドから TypeScript のコードを生成する
kemuridama
0
250
cloudnative conference 2026 flyle
azihsoyn
1
190
ふにゃっとしない名前の付け方 〜哲学で茹で上げる、コシのあるソフトウェア設計〜
shimomura
0
120
Hive Metastoreを通して学ぶIceberg REST Catalog ― 仕様から実装まで
okumin
0
210
ソースコード→AST→オペコード、の旅を覗いてみる
o0h
PRO
1
140
RailsTokyo 2026#4: AI様があれば、 Hotwireの弱点は消えるか?
naofumi
3
440
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
5
1.7k
Agentic UI beyond Chats Architecture Patterns & Open Standards @ngMunich 05/2026
manfredsteyer
PRO
0
110
AI駆動開発勉強会 広島支部 第一回勉強会 AI駆動開発概要とワークショップ
hayatoshimiu
0
240
色即是空、空即是色、データサイエンス
kamoneggi
1
130
今さら聞けないCancellationToken
htkym
0
110
Featured
See All Featured
The Cult of Friendly URLs
andyhume
79
6.9k
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
65
54k
Music & Morning Musume
bryan
47
7.2k
Ethics towards AI in product and experience design
skipperchong
2
280
Tell your own story through comics
letsgokoyo
1
920
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
150
Building an army of robots
kneath
306
46k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
110k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
Have SEOs Ruined the Internet? - User Awareness of SEO in 2025
akashhashmi
0
340
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.2k
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