Why You Should Use a Constants File in Swift

Joe Vargas
The Startup
Published in
4 min readDec 4, 2020

It’s no surprise and it’s quite encouraged that writing clear, concise code is key in the programming world. One of my favorite methodologies that I learned in iOS development and happily adopted early on is the use of a Constants file.

What is a Constants file?

Simply put, it is what it’s called: A file dedicated to store declared constant properties. The beauty of this file is that it’s accessible globally throughout the app.

What are the benefits of a Constants file?

Key benefits of a Constants file is having a central place for managing globally and repetitively used properties, the file is highly custom and you reduce the amount of mistakes such as typing in the wrong code.

So what kind of properties can you store in the Constants file?

Let’s think of common properties you might need repeatedly while building an app. One that comes to mind are colors. While you may be able to configure these properties in Storyboards, there may be times you may need to programmatically, especially when you have designers who’d like custom colors. If you ever had to apply colors programmatically, it can be quite cumbersome.

For instance, let’s say you’re working with a designer who adamantly wants to use the colors of Indigo and Navy. Unfortunately, Xcode does not have either color pre-defined in its palette. So we do a little Google-ing and find the RGB combos for the colors. Now that we’ve found our RGB settings for these two colors, let’s apply them to label text:

Now that we fulfilled the designer’s request of specifically coloring our label text, we get an email from him saying they are moving forward in making these two colors their primary color scheme and will be applying these colors globally across the app. At this point, you can copy and paste these RGB settings to a text file to reference to or find a better way. Thankfully, there’s always a better way and it’s called a Constants file.

The anatomy of a Constants file consists of nested structs and static properties. To understand static properties in Swift, they are type properties written as part of a type’s definition, within the types outer curly braces, and each type property is explicitly scoped to the type it supports.

Let’s put this into practice. Rather than using the UIColor combo value for the textColor of the label, we want to store these color values to a Constants file to access globally and apply it to any UI element.

Add a new Swift file to your project and name it Constants.swift. Since UIColors are part of the UIKit framework, you must add import UIKit. To start building our Constants file, it is customary that the top tier level struct be named as K; naming it Constants is a bit overkill. Within the K struct, let’s add another struct called Colors. Inside this particular struct is where we’ll add our navy and indigo RGB-defined color static properties. The reason why we declare them as static is for they can be called through the K.Colors instance. Below is how your Constants file should look so far.

Now that we’ve set up our Constants file, let configure the colors of the labels declared in ViewController.swift.

Simply just apply the color values to the Label textColor attributes as so:

navyColorLbl.textColor = K.Colors.navy
indigoColorLbl.textColor = K.Colors.indigo

Notice after you type K, the nested struct of Colors populates as an option to select. Once you choose Colors, the properties of navy and indigo are at your disposal.

So as you can see, having a Constants file to cleanly and centrally store global properties can be helpful. Consider the story of Deutsche Telekom’s ‘Magenta’ trademark war with Lemonade. The T-Mobile parent company went on a vendetta to prohibit other companies from using the magenta color, even companies like Lemonade who are not even in the same industry, thus they are not direct competitors. However, in respect to the German court, Lemonade temporarily changed the color. It is my hope that they used a Constants file in their app where they defined the magenta color. Else, they would have to change it everywhere magenta was used.

I hope you found using a Constants file useful to apply to your own apps. I personally build my Constants file to organize API URLs and parameters in addition to file paths; the possibilities are endless. It really pays for itself when you sit down and structure a solid Constants file, especially when you know you’re going to have to use many properties repetitively across your app.

Happy Coding!

--

--