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?
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 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.
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.
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)
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
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 } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With