Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

swift

xcode6

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?

like image 955
MatterGoal Avatar asked Dec 03 '14 17:12

MatterGoal


People also ask

How do I view environment variables in Swift?

Use the jump bar next to the Run and Stop buttons in the project window toolbar to open the scheme editor. Select the Run step on the left side of the scheme editor. Click the Arguments button at the top of the scheme editor to access the environment variables. Use the Add (+) button to add the environment variables.

How do you set environment variables on a Mac?

Set an Environment Variable — temporary or permanent you can set an environment variable for the temporary or permanent use. It depends on the case, if you need a variable for just one time, you can set it up using terminal. Otherwise, you can have it permanently in Bash Shell Startup Script with “Export” command.

What are environment variables in UFT?

Environment variables in UFT/QTP are very special types of variables that are accessible across all actions(either local or external actions), function libraries, and recovery scenarios in a test. UFT environment variables are available only for test script during runtime and can't be accessed by any other program.


1 Answers

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
}
like image 126
Martin R Avatar answered Oct 24 '22 07:10

Martin R