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
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
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.3k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
870
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
920
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.7k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.6k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Statistics for Hackers
jakevdp
799
230k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.8k
Google's AI Overviews - The New Search
badams
0
1.1k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.4k
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