Fear of Swizzling

 

I believe that Swizzling is the most argumentative technique in Objective-C/Swift.

Apple allows developers to use it. It also has released some official documents about Swizzling. But Apple doesn’t set a standard on using Swizzling. It means the application could have some potential chances of being rejected.

I’m not a fan of Swizzling, but there was a situation that I used and harvested some serious feedbacks.

The trouble

In mid-2017, My co-worker and I re-built a key component in the application. The new component required to change the way we presented ViewController entirely app. We had almost hundreds of places to migrate to the code for presenting ViewControllers.

To resolve the issue and prevent other guys presenting ViewControllers improperly, We thought of helper methods for presenting VC


protocol AppHelper {}

extension AppHelper where Self : UIApplication {

func present(vc viewControlelr: UIViewController, animated: Bool, completion: (()->Void)? {

//Implement code for presenting ViewControllers

}

}

It looks quite good. But this thing totally changes the developer’s behavior.

They no longer do this


let popupVC = UIViewController()

present(popupVC)

They must do this instead


UIApplication.shared.present(vc: popupVC, animated: true, completion: nil)

Therefore, developers who didn’t know what we did, could implement the wrong way to present VC. We didn’t want somebody to make mistake, so We came up with a solution that applied method swizzling on UIViewController.present(…). The solution basically resolved both cases, so We didn’t have to correct code at too many places, other members kept using what they were familiar with.

Some days after checking in code, Using swizzling method led us to some serious discussions and arguments.

Some guys were afraid of getting App rejected from Apple, although our implementation basically just added some extended handling to the original implementation. They kept escalating until We accepted to remove the swizzling code.

It didn’t make sense. By removing swizzling code, We generated a ton of repeated line of code and by the way. It looked weird and unnatural for the entire project.

In my opinion, I think this is not about the implementation, but fear of swizzling is too big.
Most of the guys think swizzling technique is a real monster, so they void it at any cost.

Is it a monster? 

Monster or not monster
Monster or not monster

 

I did a research, I found out that I was not the only one who tried to apply Swizzling technique to an application.

Let take a look at AFNetworking framework, one of the most well-known open source frameworks in Github.

Swizzling in AFNetworking
AFNetworking Framework

 

I begin to find out the clues of method swizzling in the library

 

Swizzle_AFNetworking3
Swizzling in AFNetworking

 

Swizzling in AFNetworking
Swizzling in AFNetworking

 

Swizzling in AFNetworking
Swizzling in AFNetworking

 

Now let’s take a look at Facebook React Native framework

Swizzling in React Native
React Native framework

 

Swizzling in React Native
Swizzling in React Native

 

Swizzling in React Native
Swizzling in React Native

 

And I also found out that Google’s Firebase been applying Swizzling technique officially.
It also provides options so developers can decide to use swizzling or not. But they will have to do some configuration manually if turns off Swizzling.

Swizzling in Firebase
Swizzling in Firebase

 

And Look like rollout.io do something further with Swizzling. Does rollout.io use method swizzling? I don’t know. I haven’t given a conclusion but found this post “Under the Hood: How We Patch Your Mobile App in Production

Conclusion

The people keep saying that is a bad idea or bad design when it involves Swizzling technique.

But they should know there are tons of article which talk, teach and discuss this topic out there.

And they should also know that there are many frameworks applying the swizzling technique. Ei: AFNetworking, React-native, Firebase,…

So anyway, it will be a bad idea to say using swizzling technique is a bad idea.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s