Created
February 6, 2023 19:54
-
-
Save foxicode/8ab0c0c328b87f6e8b70be6208fa2e37 to your computer and use it in GitHub Desktop.
Swizzling in Swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| extension UIViewController { | |
| @objc func altViewDidAppear(_ animated: Bool) { | |
| print("View did appear - \(type(of: self))") | |
| altViewDidAppear(animated) | |
| } | |
| static func swizzle() { | |
| let originalSelector = #selector(UIViewController.viewDidAppear(_:)) | |
| let altSelector = #selector(UIViewController.altViewDidAppear(_:)) | |
| let originalMethod = class_getInstanceMethod(self, originalSelector)! | |
| let altMethod = class_getInstanceMethod(self, altSelector)! | |
| method_exchangeImplementations(originalMethod, altMethod) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment