iOS method swizzling
AntonAleksandrov 270005D80F Visits (2665)
Objective-C runtime allows to change binding between selector (method) declarations and implementations. This technique is known as selector (or method) swizzling and it allows to basically "patch" the code of selectors you don't own sources of, e.g. NSString or NSObject. Selector swizzling technique is different from creating a category class with a same selector name. When you're using the latter technique you're basically fully overriding the original method implementation. Selector swizzling, on the other hand, allows calling the original method implementation thus gaining an almost subclass/superclass behaviour.
The video below demonstrates how to use selector swizzling to
1. Swizzle selector from a custom object
2. Swizzle selector from a NSObject class while keeping and using reference to the original selector.
Use setting button to enable HD video
Keep in mind that selector swizzling allows deep intervention in the OS components. You never know when Apple might decide it is "illegal" to do so.