Method swizzling in iOS swift

What is method swizzling ?? — this was a question asked in an interview. I didn’t knew the answer back then. I googled it and found many articles about method swizzling. But, I couldn’t understand the proper use case of method swizzling. Michael Mavris’s article on method swizzling was simple and easy to understand. Read it here.

What is method swizzling?

How to do method swizzling in swift 4?

  • Create a new method with your custom implementation for a method that you want to swizzle.
  • Get the class representation.
  • Get the old method selector reference.
  • Get the new method selector reference.
  • Ask the objective-C runtime to switch the selectors.
  • Take a deep breath and relax.!! 😂.

Let’s swizzle!!

print(UIColor.red) // prints UIExtendedSRGBColorSpace 1 0 0 1
import Foundationimport UIKitpublic extension UIColor {@objc func colorDescription() -> String {return "Printing rainbow colours."}private static let swizzleDesriptionImplementation: Void = {let instance: UIColor = UIColor.redlet aClass: AnyClass! = object_getClass(instance)let originalMethod = class_getInstanceMethod(aClass, #selector(description))let swizzledMethod = class_getInstanceMethod(aClass, #selector(colorDescription))if let originalMethod = originalMethod, let swizzledMethod = swizzledMethod {
// switch implementation..
method_exchangeImplementations(originalMethod, swizzledMethod)
}}()public static func swizzleDesription() {_ = self.swizzleDesriptionImplementation}}
override func viewDidLoad() {super.viewDidLoad()print(UIColor.red)print(UIColor.green)UIColor.swizzleDesription()print(“\nswizzled\n”)print(UIColor.red)print(UIColor.red)UIColor.swizzleDesription()print(“\nTrying to swizzle again\n”)print(UIColor.red)print(UIColor.red)}
UIExtendedSRGBColorSpace 1 0 0 1UIExtendedSRGBColorSpace 0 1 0 1swizzledPrinting rainbow colours.Printing rainbow colours.Trying to swizzle againPrinting rainbow colours.Printing rainbow colours.

Drawbacks of method swizzling

  • If you are using swizzling any standard class methods, then better make sure that the swizzling is successful and your new swizzled method is getting called. If you are using some frameworks (ex: Firebase) which uses swizzling ,make sure that you are not swizzling some method that they already swizzled . If swizzling happens multiple times , either your code won’t work ,or the firebase (or any other framework swizzling the same method) won’t work.
  • When newer iOS versions are released, there are chances that the swizzling fails. You may have to cross check this every time.
  • If you are shipping a framework (ex: some analytics framework) which is used by hundreds of apps, better not to use swizzling in this case. In case if you want to use swizzling, please make sure that the app developer is aware of the swizzling that the framework does. Make sure that it is added to the documentation.
  • Swizzling inside a subclass will be a headace. unexpected things might happen.

If you enjoyed reading this post, please share and give some clapps so others can find it 👏👏👏👏👏 !!!!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store