Swift Best Practices: Enums and Extensions

Hithakshi Kulal
2 min readAug 5, 2019
Photo by Clément H on Unsplash

When I started learning swift, first thing my mentor told me to follow best practices. And I never regret following his guidance.

So while doing one particular project which involved coordinator pattern, I came across one such problem. Not a huge problem, but a problem that matters. Specifying colour, images for views and cells. Question arises where to keep it and how easy it is to access.

Initially, I kept it in cell class since that colour and images was used only in that. But as project grew, things were repeated in so many cells and view controllers.

One such example: I had to bookmark a particular product and change images accordingly. Basically I had 3–4 images. say e.g

  1. placeholder
  2. filled
  3. unfilled
  4. disabled

All these are stored in asset file. But how to organise it better!

Extension to the rescue!!!

Usage:

But we can do it still better.

Magic of Enums !!!

Usage:

What’s the use?

Better readability and better organisation. !

--

--