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
180
Claude codeで 創る世界と壊れる世界
codelynx
0
220
Caude codeで爆速開発
codelynx
0
190
2024-05-yumemi.pdf
codelynx
0
400
ユーザーたった一人のアプリ開発記
codelynx
0
370
Hello New Project
codelynx
1
310
WWDC21/Xcode13
codelynx
0
270
Tailor_your_Metal_apps_for_Apple_M1.pdf
codelynx
0
140
Delegate Chaining Workaround
codelynx
0
79
Featured
See All Featured
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
210
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
1
540
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
210
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.5k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
860
Claude Code のすすめ
schroneko
67
230k
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
180
Amusing Abliteration
ianozsvald
1
210
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.1k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
390
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
330
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