Global constants file in Swift Global constants file in Swift swift swift

Global constants file in Swift


Structs as namespace

IMO the best way to deal with that type of constants is to create a Struct.

struct Constants {    static let someNotification = "TEST"}

Then, for example, call it like this in your code:

print(Constants.someNotification)

Nesting

If you want a better organization I advise you to use segmented sub structs

struct K {    struct NotificationKey {        static let Welcome = "kWelcomeNotif"    }    struct Path {        static let Documents = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String        static let Tmp = NSTemporaryDirectory()    }}

Then you can just use for instance K.Path.Tmp

Real world example

This is just a technical solution, the actual implementation in my code looks more like:

struct GraphicColors {    static let grayDark = UIColor(0.2)    static let grayUltraDark = UIColor(0.1)    static let brown  = UIColor(rgb: 126, 99, 89)    // etc.}

and

enum Env: String {    case debug    case testFlight    case appStore}struct App {    struct Folders {        static let documents: NSString = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString        static let temporary: NSString = NSTemporaryDirectory() as NSString    }    static let version: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as! String    static let build: String = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as! String    // This is private because the use of 'appConfiguration' is preferred.    private static let isTestFlight = Bundle.main.appStoreReceiptURL?.lastPathComponent == "sandboxReceipt"    // This can be used to add debug statements.    static var isDebug: Bool {        #if DEBUG        return true        #else        return false        #endif    }    static var env: Env {        if isDebug {            return .debug        } else if isTestFlight {            return .testFlight        } else {            return .appStore        }    }}


I am abit late to the party.

No matter here's how i manage the constants file so that it makes more sense to developers while writing code in swift.

FOR URL:

//URLConstants.swift  struct APPURL {    private struct Domains {        static let Dev = "http://test-dev.cloudapp.net"        static let UAT = "http://test-UAT.com"        static let Local = "192.145.1.1"        static let QA = "testAddress.qa.com"    }    private  struct Routes {        static let Api = "/api/mobile"    }    private  static let Domain = Domains.Dev    private  static let Route = Routes.Api    private  static let BaseURL = Domain + Route    static var FacebookLogin: String {        return BaseURL  + "/auth/facebook"    }}

For CUSTOMFONTS:

//FontsConstants.swiftstruct FontNames {    static let LatoName = "Lato"    struct Lato {        static let LatoBold = "Lato-Bold"        static let LatoMedium = "Lato-Medium"        static let LatoRegular = "Lato-Regular"        static let LatoExtraBold = "Lato-ExtraBold"    }}

FOR ALL THE KEYS USED IN APP

//KeyConstants.swift    struct Key {        static let DeviceType = "iOS"        struct Beacon{            static let ONEXUUID = "xxxx-xxxx-xxxx-xxxx"        }        struct UserDefaults {            static let k_App_Running_FirstTime = "userRunningAppFirstTime"        }        struct Headers {            static let Authorization = "Authorization"            static let ContentType = "Content-Type"        }        struct Google{            static let placesKey = "some key here"//for photos            static let serverKey = "some key here"        }        struct ErrorMessage{            static let listNotFound = "ERROR_LIST_NOT_FOUND"            static let validationError = "ERROR_VALIDATION"        }    }

FOR COLOR CONSTANTS:

//ColorConstants.swiftstruct AppColor {    private struct Alphas {        static let Opaque = CGFloat(1)        static let SemiOpaque = CGFloat(0.8)        static let SemiTransparent = CGFloat(0.5)        static let Transparent = CGFloat(0.3)    }    static let appPrimaryColor =  UIColor.white.withAlphaComponent(Alphas.SemiOpaque)    static let appSecondaryColor =  UIColor.blue.withAlphaComponent(Alphas.Opaque)    struct TextColors {        static let Error = AppColor.appSecondaryColor        static let Success = UIColor(red: 0.1303, green: 0.9915, blue: 0.0233, alpha: Alphas.Opaque)     }    struct TabBarColors{        static let Selected = UIColor.white        static let NotSelected = UIColor.black    }    struct OverlayColor {        static let SemiTransparentBlack = UIColor.black.withAlphaComponent(Alphas.Transparent)        static let SemiOpaque = UIColor.black.withAlphaComponent(Alphas.SemiOpaque)        static let demoOverlay = UIColor.black.withAlphaComponent(0.6)    }}

You can wrap these all files in a common group named Constants in your Xcode Project.

And for more watch this video


Although I prefer @Francescu's way (using a struct with static properties), you can also define global constants and variables:

let someNotification = "TEST"

Note however that differently from local variables/constants and class/struct properties, globals are implicitly lazy, which means they are initialized when they are accessed for the first time.

Suggested reading: Global and Local Variables, and also Global variables in Swift are not variables