Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created February 6, 2023 19:54
Show Gist options
  • Select an option

  • Save foxicode/8ab0c0c328b87f6e8b70be6208fa2e37 to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/8ab0c0c328b87f6e8b70be6208fa2e37 to your computer and use it in GitHub Desktop.
Swizzling in Swift
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