Using Global Constant Like A Pro

Introduction 

In our code, most of the time we use constants (Ex: cell Identifier, some static text, maybe certain height). It’s a good practice to use constants instead of variables whenever possible. Since their value can’t be changed afterward, it’s much safer and the compiler can make optimizations if the system knows certain data won’t change. It is always advisable to declare all the constants in a global file and access those constant from the global file itself. This technique has lots of advantages. Consider, we are using one constant "My Name" in 4 to 5 View Controllers. Then, after that we realize that constant should be "My Name is :", we need to go to all view controllers and search for that particular constant. Now, assume we have added the global constant somewhere in a Swift file like :

  1. let kMyNameConstant = " My Name "  
and using that constant in all 4 to 5 view controllers. In this case, instead of editing all the view controllers, we will be editing in a single constant file.
 
Here, we will see one of the smartest ways to declare and access the global constant throughout your iOS Project.
 
System Requirments  
  • Xcode 9.0 , Swift 4.0 . 
  • Expertise level: intermediate. 
Step 1

Create one blank iOS project. Add a new Swift file to it - GloablConstant.swift

 
 
Step 2

You must know how to create a Swift constant.
  1. let number: Int = 5  
Swift 4 provides a flexible building block for making use of constructs as Structures. By making use of these structures, one can define constructs methods and properties.
 
Now, we will create a structure of GlobalConstant Class. Within that, we will be using structure to categories to a different type of constant. We can have a different type of constant. That may be a cell Identifier, Maybe some height or width. Maybe some static string. Or maybe some particular color.
  1. import Foundation  
  2. import UIKit  
  3.   
  4. struct GlobalConstants {  
  5.       
  6.     struct CellIdentifier {  
  7.         static let kCollectionViewCellID = "MyCollectionViewCellID"  
  8.         static let kTableViewCellID = "MyTableViewCellID"  
  9.     }  
  10.       
  11.     struct Color {  
  12.         static let kBackgroundColor: UIColor = UIColor.init(red: 20/255, green: 40/255, blue: 50/255, alpha: 0)  
  13.         static let kTableCellBgColor: UIColor = UIColor.blue  
  14.     }  
  15.       
  16.     struct CellHeight {  
  17.         static let kCollectionViewCellHeight = 50  
  18.         static let kTableViewCellHeight = 18  
  19.     }  
  20.       
  21. }  

 
Step 3

Now, we will access those constants in our view controller class. Go to ViewController.swift -> ViewDidLoad Function . Write the below code to access all the constants in your view controller. 

  1. override func viewDidLoad() {  
  2.     super.viewDidLoad()  
  3.     // Do any additional setup after loading the view, typically from a nib.  
  4.       
  5.     self.view.backgroundColor = GlobalConstants.Color.kBackgroundColor // set background color  
  6.       
  7.     print(GlobalConstants.CellHeight.kCollectionViewCellHeight) // print collection view cell height  
  8.       
  9.     print(GlobalConstants.CellIdentifier.kCollectionViewCellID) // print collection view cell Identifier  
  10. }  

Conclusion
 
Here is the simple trick of how we can separate our constants, declare them globally in a single find, and be able to access them more precisely in an effective way. Hope you will be using this for your next big project. Find the source code below. Let me know in comments if any clarification required. Happy Programming !!! :)


Similar Articles