Start Using Helper Class Now! — Part 1

Globally reusable constants and more…

Mohaiminul Islam
InfancyIT
4 min readMar 12, 2018

--

As iOS developers, we are used to write native UI component configuration codes almost in all of our view classes. Most of the time they are color literals for backgroundColor, textColor, tintColor, borderColor for component layer etc. We use them in view classes as cell models and sometimes we override these parameters in code from our controllers and extensions.

This approach is fine and works in many situations, but what if you opt for a standard color theme with just two main colors? Let’s assume you use one for background or main app color and another for the highlighted parts on that background color (e.g. backgroundColor, textColor, tintColor), we’ll have to rewrite the same color literals or codes in all the places we want them to be implemented.

Let’s take a look at a sample view class, in this case a TableViewCell class to be exact.

A typical medium sized app may have at least 5 to approximately 16 cell classes and god knows how many ViewControllers/TableViewControllers/ CollectionViewControllers (In my case, I always end up with 7–11 ViewControllers in small apps and more than 20 ViewController classes in medium to large apps while using MVC). We always want our app to be correlated in form and color throughout the whole app. So, we have to duplicate those constants all over those places.

Even if you think it’s manageable to redeclare color and other constants in this way, every time when you need it somewhere else; imagine what if, you have to change the colors after you’re done placing all those constants.

You WILL have to redo all those lines of code where you declared those constants!

Instant Doomsday!

It’s not rare at all! Actually, even if a design is given prior developing starts, a design update might happen once the app is almost finished. Also you might want to play with changing the color and see what theme suits the app.

To avoid having to rewrite constants in all those places, I’ve implemented a neat way learned from my OOP days while learning programming. I have a helper file named Helper.swift almost in all of my projects. There lies a simple class structure with my globally used variables like below.

This way, whenever we need any of our global variables/constants, we can just write,

Helper.app.primaryColor   // returns UIColor.white
Helper.app.secondaryColor // returns UIColor.black.alpha(0.7)

Now we can just use the constants defined in our Helper file all over our classes like so. Form the case state before, we can rewrite the cell class now like below,

What is so great about this approach alongside readability and reusability perks, now, if we want to change any of those constant values (in this case colors), we just modify our Helper.swift file’s constant value and it’s reflected in all our implementations. We don’t need to do anything else! At last, we get rid of those annoying edits all over the files everytime we want to change a simple constant value globally.

Great relief awaits you if you use those global constants!

Let’s change the value of some constants to see the result.

Now the variables would reflect the newly assigned values.

Helper.app.primaryColor   // will return UIColor.blue.alpha(0.3)
Helper.app.secondaryColor // will return UIColor.white.alpha(0.7)

.

.

.

PRO DEV TIP:

You can use this class to override all the nav controller’s title text attributes if you want to change ViewController title text’s font, colors etc.

.

.

First, we need to add the titleTextAttribute constant in our Helper class. Add the following line at the end of the code block inside the class.

Now, whenever you need this attribute for styling your navbar & title, just create a UINavigationController class for your view’s nav controller, and inside viewDidLoad() after super.viewDidLoad() add the following lines,

Easy peasy.

Now that’s over with, we should not limit ourselves to just assigning variables and constants in here. We can also write functions - those we need to use globally to avoid rewriting them all.

Continue reading on how to utilize on global functions on the followup article!

That’s it for now. Check out my other articles here.

--

--

Mohaiminul Islam
InfancyIT

Software Engineer (iOS, Web) & a reclusive learner