问题
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
}
来源:https://stackoverflow.com/questions/27278303/creating-environment-variables-in-xcode-6-0-schema-and-obtaining-them-back-from