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
}
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…