在我的Objective-C项目中,我经常使用一个全局常量文件来存储像通知名称和NSUserDefaults键这样的东西。它看起来是这样的:

@interface GlobalConstants : NSObject

extern NSString *someNotification;

@end

@implementation GlobalConstants

NSString *someNotification = @"aaaaNotification";

@end

我如何在Swift中做完全相同的事情?


当前回答

结构作为命名空间

在我看来,处理这类常量的最好方法是创建一个Struct。

struct Constants {
    static let someNotification = "TEST"
}

然后,例如,在你的代码中这样调用它:

print(Constants.someNotification)

嵌套

如果你想要一个更好的组织,我建议你使用分段子结构

struct K {
    struct NotificationKey {
        static let Welcome = "kWelcomeNotif"
    }

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

然后您可以使用实例K.Path.Tmp

现实世界的例子

这只是一个技术解决方案,在我的代码中的实际实现看起来更像:

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
        }
    }
}

其他回答

或者只在GlobalConstants.swift中:

import Foundation

let someNotification = "aaaaNotification"

结构作为命名空间

在我看来,处理这类常量的最好方法是创建一个Struct。

struct Constants {
    static let someNotification = "TEST"
}

然后,例如,在你的代码中这样调用它:

print(Constants.someNotification)

嵌套

如果你想要一个更好的组织,我建议你使用分段子结构

struct K {
    struct NotificationKey {
        static let Welcome = "kWelcomeNotif"
    }

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

然后您可以使用实例K.Path.Tmp

现实世界的例子

这只是一个技术解决方案,在我的代码中的实际实现看起来更像:

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
        }
    }
}

根据swift docs,全局变量是在文件作用域中声明的。

全局变量是定义在任何函数、方法、闭包或类型上下文之外的变量

只需创建一个swift文件(例如:constats .swift)并在那里声明你的常量:

// Constants.swift

let SOME_NOTIF = "aaaaNotification"

从项目中的任何地方调用它,而不需要提及struct,enum或类名。

// MyViewController.swift

NotificationCenter.default.post(name: SOME_NOTIF, object: nil)

我认为这对于代码可读性来说更好。

我在Swift项目中所做的 1:新建Swift File 2:创建一个结构和静态常量。 3:对于使用只使用YourStructName.baseURL

注意:在创建初始化后,初始化需要很少的时间,所以它将在2-5秒后显示在其他视图控制器中。

import Foundation

    struct YourStructName {
    static let MerchantID = "XXX"
    static let MerchantUsername = "XXXXX"
    static let ImageBaseURL = "XXXXXXX"
    static let baseURL = "XXXXXXX"
    }

虽然我更喜欢@Francescu的方式(使用带有静态属性的结构体),但你也可以定义全局常量和变量:

let someNotification = "TEST"

但是请注意,与局部变量/常量和类/结构属性不同,全局变量是隐式惰性的,这意味着它们在第一次访问时被初始化。

建议阅读:全局变量和局部变量,以及Swift中的全局变量都不是变量