Swift: Adopt Protocols without cluttering the class declaration

After transitioning to Swift and Protocol Orietented Programming you might find yourself in a situation where your ViewControllers adopt a lot of protocols

class MYViewController: UIViewController, AppearanceProvider, ZoomingBehaviour, ThirdProtocol, UINavigationDelegate {

}

The more protocols you add the uglier this chain of comma separated protocols gets. The cheap solution (comes from a good friend of ours) is to create an extension for each protocol you want to adopt(implement)and to keep the code related to that protocol in the same extension. This provides a nice Interface Segregation and separation of responsibilities.

class MYViewController: UIViewController {  
}

extension MYViewController: AppearanceProvider {  
// appearance code
}

extension MYViewController: ZoomingBehaviour {  
// zooming code
}

extension MYViewController: UINavigationDelegate {  
// navigation code
}

The Code Bug

A passionate iOS developer. Looking to radically improve the way we all develop software.

Amsterdam