MatterGoal MatterGoal - 3 months ago 19
Swift Question

Creating environment variables in Xcode 6.0 schema and obtaining them back from code in swift

How can I add some environment variables through a schema and then retrieve those variables using code?

For example I want to add an environment variable to describe the "exec_mode" like "development" or "production"... and I though to add this variable directly into the schema "environment variables". Now how can I get this variable back into my code in Swift?

Answer

You can get the environment variables with NSProcessInfo:

let env = NSProcessInfo.processInfo().environment
if let mode = env["exec_mode"] as? String {
    print(mode)
} else {
    // Environment variable not set
}

Swift 3:

let env = ProcessInfo.processInfo.environment
if let mode = env["exec_mode"] {
    print(mode)
} else {
    // Environment variable not set
}