Wesley de Groot's Blog
Snippet: @EnvironmentVariable


Snippet: @EnvironmentVariable

If you are creating commandline apps you sometimes need to acces the operating system environment variables, while you can use getenv(name) it can come in handy to use a property wrapper if you need to access the environment variables often, or if you are updating them.

Full code:

/// A  property wrapper to set and get system's environment variables values.
/// ```
/// @EnvironmentVariable(name: "PATH")
/// var path: String?
/// ```
public struct EnvironmentVariable {
    var name: String

    public var wrappedValue: String? {
        get {
            guard let pointer = getenv(name) else { return nil }
            return String(cString: pointer)
        set {
            guard let value = newValue else {

            setenv(name, value, 1)

in the future i'll post more about @propertyWrappers.

x-twitter mastodon github linkedin discord threads instagram whatsapp bluesky square-rss sitemap