Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global constants file in Swift

In my Objective-C projects I often use a global constants file to store things like notification names and keys for NSUserDefaults. It looks something like this:

@interface GlobalConstants : NSObject  extern NSString *someNotification;  @end  @implementation GlobalConstants  NSString *someNotification = @"aaaaNotification";  @end 

How do I do exactly the same thing in Swift?

like image 927
user1028028 Avatar asked Oct 08 '14 08:10

user1028028


People also ask

How do you declare a global constant?

A constant which is needed in more than one functions can be declared a global constant by declaring it a constant using the reserve word const, initializing it and placing it outside of the body of all the functions, including the main function.

What does constant file mean?

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 keyword is used to define a constant in Swift?

Swift employs the keywords let and var for naming variables. The let keyword declares a constant, meaning that it cannot be re-assigned after it's been created (though its variable properties can be altered later). The var keyword declares a new variable, meaning that the value it holds can be changed at a later time.


1 Answers

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         }     } } 
like image 178
Francescu Avatar answered Sep 28 '22 02:09

Francescu