Fear of Swizzling

 

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

Apple makes it available to use. Apple also releases some documents about Swizzling, it doesn’t set a standard for using the Swizzling technique. Hence the application could have some potential risk to pass the app review.

I’m not a fan of Swizzling, but I was in a situation of using the swizzling technique and finally harvested some serious feedbacks.

The trouble

In mid-2017, My co-worker and I re-built a key component in the application. According to the component, We must change the way that presents ViewController entirely app. There were almost a bunch of places in the app which present a viewController.

To resolve the issue and prevent other guys presenting ViewControllers inappropriately, 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 don’t have to write this code


let popupVC = UIViewController()

present(popupVC)

They must do this instead


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

The developers who didn’t know what we did, could implement the wrong way to present VC. We didn’t want anybody to make mistake, so We came up with a solution that applied method swizzling into UIViewController.present(…). The solution basically solved both cases. We didn’t have to correct code in many places, my colleagues 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 gave up the swizzling code.

It didn’t make sense. By removing swizzling code, We generated a ton of repeated line of code. It looked bad.

From my point, I think this is not about the coding, but fear of swizzling is too big.
Most of the guys think swizzling technique is a monster, so they void it anyway.

Is it a monster? 

monster-or-not-monster2.png

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

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 did some things which use the Swizzling technique. I 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 implementing the swizzling technique as well. So why We negative it.

 

 

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s