Use of Swift Extensions in an expert way

Let’s dress up the code…

Sagar More
4 min readNov 8, 2018

Extensions add new functionality to an existing class, structure, enumeration, or protocol type. This includes the ability to extend types for which you do not have access to the original source code (known as retroactive modeling). Extensions are similar to categories in Objective-C. (Unlike Objective-C categories, Swift extensions do not have names.)

I assume you have a basic understanding of Swift Extension. So let’s start with the actual use of it.

  • Protocol Conformance :

Swift programming language says that Extensions can be used to confirm an existing type to a protocol. It also helps you to keep the code organised.

Take an example of UITableviewDelegate & UITableViewDataSource method implementation. It results in lengthy class implementation and it also becomes difficult to navigate over time.

You can keep your code organised by separating each protocol separate to which type it conforms to. This also enables easy navigation to files, if you use jump bar which resides on top on source code editor.

Protocol conformance with Extensions
  • Default Protocol Implementation :

When we adopt any protocol, then need to provide the implementation for the methods, properties of protocol in our class/struct.

But in some cases, we want to give the default implementation for protocol so that class in which protocol is adopted don’t need to provide protocol implementation again. We simply achieve this by extending protocol and provide default implementation to methods/properties in its extension.

Default protocol implementation
  • Preserving Initializers :

Struct implicitly creates an initialiser for us, which can be used to instantiate the struct object. Unfortunately, default initializer is not available, if you define custom initializer in struct definition, as shown in below graphics.

Fortunately, we have an easy workaround to resolve this issue, by creating an extension for struct and define custom initializer over there. In this way, we can have default initializer as well as a custom one.

Preserving Initializers
  • Nested Types :

Extensions in Swift also allows you to use & define Nested Types. But I feel this feature is undervalued. We can use it to define constant like below example

Nested Types with Extension
  • Computed Properties :

Have you come across the situation, where you wanted to add properties in Extensions? There is a workaround for the same too.

You can not add stored properties in Extension, but you can add computed properties. See below example for same

  • Code Separation :

We are taking previous Employee extension example one step further.

We can use Swift extensions to separate state from behaviour. If we applied this technique to our previous example, we will end up something like below :

Type definition only defines the stored properties. The extension is created to define the behaviour of a type, i.e. method & computed properties.

We can take this to one step ahead by creating a private extension for private behaviour. In this way, we can easily do code separation with the help of extensions.

Code Separation

If you liked this post, please share & give some clapps 👏 !

You can follow me on Medium for fresh articles. Also, connect with me on LinkedIn .

happyCoding()-> 😃

--

--

Sagar More

Sr. iOS Developer @ Delivery Hero | Mobile Enthusiast | Swift Expert