Referring to Environment Variables in Swift

SwiftEnvironment Variables

Swift Problem Overview


Does anybody know how I can grab an environment variable in Swift?

Swift Solutions


Solution 1 - Swift

Swift 3 and up:

import Foundation

if let value = ProcessInfo.processInfo.environment["KEY"] {
    ...
}

Solution 2 - Swift

Along with the NSProcessInfo-based method mentioned by Oleg, you can access environment variables using the the standard C getenv function, like so:

Swift 2.x:

func getEnvironmentVar(name: String) -> String? {
    let rawValue = getenv(name)
    guard rawValue != nil else { return nil }
    return String(UTF8String: rawValue)
}

Swift 3.0:

func getEnvironmentVar(_ name: String) -> String? {
    guard let rawValue = getenv(name) else { return nil }
    return String(utf8String: rawValue)
}

It's also possible to set environment variables using the setenv function:

func setEnvironmentVar(name: String, value: String, overwrite: Bool) {
    setenv(name, value, overwrite ? 1 : 0)
}

The reason I mention the ability to set variables is because it's the only way I know of to set variables when working in an Xcode Playground.

I recently wanted to see a backtrace for a strange CGContext error I was getting when working with a "live" view in a playground. By default, the backtrace isn't shown in the console, so I had to set the CG_CONTEXT_SHOW_BACKTRACE environment variable to see what was up:

setenv("CG_CONTEXT_SHOW_BACKTRACE", "1", 1)

After that, it was smooth sailing. Well, other than the CGContext error I was getting, but that's a topic for another day.

Hope this helps!

P.S. The above functions require the Darwin module to be imported. Foundation automatically imports Darwin, but I thought I should mention it nonetheless.

Solution 3 - Swift

Yes it is possible. Use ProcessInfo for that.

Simple example :

let dic = ProcessInfo.processInfo.environment
if dic["VAR"] != nil {
        
}

Solution 4 - Swift

Since Swift 3 NSProcessInfo has been renamed to ProcessInfo. And method processInfo() has been replaced with property processInfo.

import Foundation

for (key, value) in ProcessInfo.processInfo.environment {
    print("\(key): \(value)")
}

Solution 5 - Swift

A Vapor specific solution:

From the code which Vapor automatically A Vgenerates as a project template:

let variable = Environment.get("VARIABLE") ?? "or-the-dafault"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionReiss JohnsonView Question on Stackoverflow
Solution 1 - SwiftemlaiView Answer on Stackoverflow
Solution 2 - SwiftBen StockView Answer on Stackoverflow
Solution 3 - SwiftOleg GordiichukView Answer on Stackoverflow
Solution 4 - SwiftRoman ShamritskiyView Answer on Stackoverflow
Solution 5 - SwiftEerik Sven PuudistView Answer on Stackoverflow