[iOS][Swift] 意外と知らない? スマートなグローバル変数ファイルの作り方


以下のように構造体を作成してから、

Constant.swift

struct K {

    struct UserDefaultsKey {
        static let Settings = "kSetings"
    }

    struct NotificationKey {
        static let Welcome = "kWelcomeNotif"
    }

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

以下のように使うだけ。

K.UserDefaultsKey.Settings
K.Path.Documents

参考:
https://stackoverflow.com/a/26252377