Wednesday 6 May 2015

OC: Swizzling: Category a class and override a method, not able to call super method, so Swizzling is the solution for you.

Hello Objective C, Apple iOS / Mac lovers,

Swizzling a technique may be useful for you in some scenario. Think a case If a super class method you want to override, but problem with that, That super class objects are already created in project. 
Now we have two ways: 
First: Traditional way, inherit that super class and override that method. But problem with this way is you have to manually change the new child class name from super class name. 
Class C1 has a method M1 which we want to override,
We made another class C2 : C1, and override method M1. 
In your project there are already many other classes have a object of C1. 
C1 *c11, *c12, *c13;
Now in find all class object which is Made of C1 and now replace to C2,
C2 *c11, *c12, *c13;
Don't you think it is too much of laborious work. Now consider the second way,

Second: Category concept in Objective C, Ok you can Category class C1 and override method M1. But Now there is another problem arises. You cannot call original method C1, because it is replaced by override method. It does mean, If you have 15 lines of code in M1 in C1, and Category CT1 override method M1, that 15 lines of codes is not available. So you have now two ways to solve:
A: You can rewrite those 25 lines of code in Category CT1 method M1. which is like again laborious work, less laborious but still you are a labour.
B: (RECOMMENDED Swizzling) Choose Swizzling technique to achieve that. In simple word, inheritance and category concept is now mix. In category if you will able to call super method then you don't have to write those 15 lines of code. This technique may be good for you if you are thinking of custom modification in lots of methods and you don't want to inherit because class name change is really pain. And you don't want to copy paste code of your super class overridden method. 
I think it is very approachable concept