Creating environment variables in Xcode 6.0 schema

2019-04-21 21:43发布

问题:

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?

回答1:

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
}


标签: swift xcode6