DevPrep

All topics › Objective-C

What is method swizzling and when to use it?

Senior PRO

Essence

Swizzling is replacing the implementation (IMP) of an existing method at runtime through the Obj-C runtime API. Most often it is swapping the IMPs of two selectors with method_exchangeImplementations, less often a direct replacement with method_setImplementation while keeping the old IMP. The effect is global: all instances of the class change, and of all subclasses that did not override the method.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store