Swift extensions

Extension what?

I started to fiddle with Swift as soon as it was publicly released. In a few days I realised that development was “swifter”, more comfortable and easier on the eyes than Objective-C.

Something that caught my attention, and love, were Extensions. Extensions allow you to extend native types, classes and Protocols whereas with Objective-C you would have to use categories, which are more limited (and boring).

Another thing that I love in extensions is the code organisation, with Swift you can segment a Class in a single or in multiple files without being restricted to “Marks”. This ability enables you to use TableViews and other kinds of delegates in a cleaner 🚿 and intuitive way.

Motivation

Shipping faster⛵️! That was it. I started to push new iOS developments with Swift because I realised that in the long run it would save a lot of dev time.

What it can bring to the table

— Protocol extensions

After a few weeks digging into swift, some Blog posts and talks specially this one in the WWDC. I realised that swift was designed to be a protocol oriented language where you can enhance Protocols with Generics and Extensions.

Protocol oriented language, what does it mean?

It means that is preferable to add functionalities to objects via Protocol rather than subclassing. With protocols extensions we can add default implementations to required methods, lets see an example.

Let’s create a protocol t0 make UICollectionViewCells reusable.

We will need:

Reuse identifier, to register and dequeue the cells

UINib from class name, to register the cell in the UICollectionView

ReusableCell protocol decalration

Now let’s see how the default implementation works for the UICollectionViewCells:

ReusableCell + NSObject extensions

Now let's add an extension to the UICollectionView to simplify the register and dequeue of the cells as well.

UICollectionView+Resusable extensios

To wrap up, we will create a UIViewController with a UICollectionView to register a custom UICollectionViewCell that is ReusableCell and dequeue it.

Register and dequeue Reusable cells in a ViewController

Warning: you need to have a Xib with the same name as the parent class of the UICollectionViewCell otherwise it will not work.

— Protocol associated types (Generics)

Let’s introduce another great feature of protocols combined with extensions that was impossible to achieve with Categories, use of associated types in protocols.

This example was taken from other Blog post, and it shows how use protocols in order to remove boilerplate when it comes to show and identify Segues in our UIViewControllers.

SegueHandler + UIViewController extension

Now let’s see how this connects with our ExampleViewController we will have two segues in the ViewController and we will present them via our protocol.

Example of a ViewController with

From now on working with Segues and UICollectionViewCells will never be the same.

— Default system types example: Strings

Another cool feature we can achieve by extending System classes is localization which is a bit boring but we can leverage Extensions to make our lives a lot easier.

Let’s take the CocoaPod Localize-Swift as an example on how to add new functionalities and minimize the boilerplate code to localize text.

String extensions in Localizable-Swift

As we can see Localize.currentLanguage() is being used to get the user selected language, a simple wrapper to support realtime language selection.

Let’s dive deep into it:

It is possible to list the available languages and set a new current language, after that all the new calls to the localized resource will be translated to the selected language.

Let’s just put it all together with a really simple example:

Warning

Now let’s see some downsides of extending

Readablity

Extensions allow a class to be segmented into many files, so be careful and do it responsibly because you might be hiding some vital pieces of behaviour.

Reliability

Overriding current implementations with extensions. After a Framework is imported in a file all of it’s extensions will be imported into your project workspace. Ex: after you import the Localize-Swift in any class the String.localized() will become available on all projects. If Apple adds this specific method to String you will never get that method called until you remove Localize-Swift.

Conclusion

Extensions will help you a lot but if you don't use it wisely they can also be a pain if not managed correctly.

Side note, we are organizing the first mobile.lx Meetup, next week (May 19) in Lisbon, so if you are around come and join us!