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
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
190
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
190
Claude codeで 創る世界と壊れる世界
codelynx
0
230
Caude codeで爆速開発
codelynx
0
200
2024-05-yumemi.pdf
codelynx
0
410
ユーザーたった一人のアプリ開発記
codelynx
0
370
Hello New Project
codelynx
1
310
WWDC21/Xcode13
codelynx
0
270
Tailor_your_Metal_apps_for_Apple_M1.pdf
codelynx
0
150
Delegate Chaining Workaround
codelynx
0
81
Featured
See All Featured
Marketing to machines
jonoalderson
1
5.6k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
エンジニアに許された特別な時間の終わり
watany
107
250k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Exploring anti-patterns in Rails
aemeredith
3
440
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Done Done
chrislema
186
16k
Heart Work Chapter 1 - Part 1
lfama
PRO
8
36k
A designer walks into a library…
pauljervisheath
211
24k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
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