Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Swizzling NSObject classes in Swift
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
codelynx
March 16, 2021
220
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swizzling NSObject classes in Swift
codelynx
March 16, 2021
More Decks by codelynx
See All by codelynx
AI & Enginnering
codelynx
0
230
Claude codeで 創る世界と壊れる世界
codelynx
0
260
Caude codeで爆速開発
codelynx
0
230
2024-05-yumemi.pdf
codelynx
0
430
ユーザーたった一人のアプリ開発記
codelynx
0
400
Hello New Project
codelynx
1
340
WWDC21/Xcode13
codelynx
0
300
Tailor_your_Metal_apps_for_Apple_M1.pdf
codelynx
0
170
Delegate Chaining Workaround
codelynx
0
94
Featured
See All Featured
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.3k
Claude Code のすすめ
schroneko
67
230k
Ruling the World: When Life Gets Gamed
codingconduct
0
440
Paper Plane
katiecoart
PRO
5
55k
From π to Pie charts
rasagy
1
440
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
340
Building an army of robots
kneath
307
47k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
350
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
2
510
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3.1k
Docker and Python
trallard
47
4.3k
Transcript
Swizzing NSObject classes Kaz Yoshikawa FEB 2021
Swizzling • To be able to replace or to override
existing NSObject methods • UIViewController viewDidAppear UIViewController myViewDidAppear viewDidAppear Before After viewDidAppear viewDidAppear
What’s for? • Analytics for user tracking • eg. Logging
extra information of all viewDidAppear methods • Application wide behavior changes • Tap on UIButton to speech before sendAction() get invoked
Example func swizzleMethod(_ `class`: AnyClass, _ original: Selector, _ swizzled:
Selector) { if let original = class_getInstanceMethod(`class`, original), let swizzled = class_getInstanceMethod(`class`, swizzled) { method_exchangeImplementations(original, swizzled) } else { print("failed to swizzle: \(`class`.self), '\(original)', '\(swizzled)'") } } swizzleMethod( UIViewController.self, #selector(UIViewController.viewDidAppear(_:)), #selector(UIViewController.viewDidAppear_x(_:)) ) public extension UIViewController { @objc private func viewWillAppear_x(_ animated: Bool) { print(Self.self, #function) self.viewWillAppear_x(animated) } }
Consideration • Do not abuse • Apple may terminate or
discontinue this mechanism in the future
Code • https://gist.github.com/codelynx/3685e0530607746a0811dfd3d1fe ff 9e
Thank you