Swift Best Practices: Enums and Extensions
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
- placeholder
- filled
- unfilled
- 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. !