Environment

@dynamicMemberLookup
public struct Environment

A type-safe interface to variables in the environment from which the process was launched.

  • Returns the default environment for the current process.

    Declaration

    Swift

    public static let environment: Environment
  • Accesses the environment variable associated with the given key for reading and writing.

    The following example sets an environment variable for "HOST" and gets the value for "HOST" from the environment.

    Environment.environment["HOST"] = "example.com"
    let host = Environment.environment["HOST"]
    

    Declaration

    Swift

    public subscript(key: String) -> String? { get nonmutating set }

    Parameters

    key

    The key used to access the environment variable.

    Return Value

    The environment variable value as a String for the given key if key is in the environment; otherwise, nil.

  • Accesses the environment variable associated with the given member for reading and writing.

    The following example sets an environment variable for "HOST" and gets the value for "HOST" from the environment.

    Environment.environment.HOST = "example.com"
    let host = Environment.environment.HOST
    

    Declaration

    Swift

    public subscript(dynamicMember member: String) -> String? { get nonmutating set }

    Parameters

    dynamicMember

    The member used to access the environment variable.

    Return Value

    The environment variable value as a String for the given dynamicMember if dynamicMember is in the environment; otherwise, nil.

  • Accesses the environment variable associated with the given key for reading and writing.

    The following example sets an environment variable for "HOST" and gets the value for "HOST" from the environment.

    Environment["HOST"] = "example.com"
    let host = Environment["HOST"]
    

    Declaration

    Swift

    public static subscript(key: String) -> String? { get set }

    Parameters

    key

    The key used to access the environment variable.

    Return Value

    The environment variable value as a String for the given key if key is in the environment; otherwise, nil.

  • Accesses the environment variable associated with the given member for reading and writing.

    The following example sets an environment variable for "HOST" and gets the value for "HOST" from the environment.

    Environment.HOST = "example.com"
    let host = Environment.HOST
    

    Declaration

    Swift

    public static subscript(dynamicMember member: String) -> String? { get set }

    Parameters

    dynamicMember

    The member used to access the environment variable.

    Return Value

    The environment variable value as a String for the given dynamicMember if dynamicMember is in the environment; otherwise, nil.

  • Accesses the environment variable associated with the given key for reading and writing and converts type T using the EnvironmentStringConvertible protocol.

    The following example gets environment variables of different types from the environment. Default values are provided using the nil-coalescing operator (??) which also enables type inference.

    let url = Environment.environment["SERVER_URL"] ?? URL(string: "https://example.com")!
    let port = Environment.environment["PORT"] ?? 80
    

    Types may also be specified explicitly.

    let duration: TimeInterval = Environment.environment["DURATION"] ?? 1
    

    Declaration

    Swift

    public subscript<T>(key: String) -> T? where T : EnvironmentStringConvertible { get nonmutating set }

    Parameters

    key

    The key used to access the environment variable.

    Return Value

    The environment variable value converted to type T using the EnvironmentStringConvertible protocol for the given key if key is in the environment; otherwise, nil.

  • Accesses the environment variable associated with the given member for reading and writing and converts type T using the EnvironmentStringConvertible protocol.

    The following example gets environment variables of different types from the environment. Default values are provided using the nil-coalescing operator (??) which also enables type inference.

    let url = Environment.environment.SERVER_URL ?? URL(string: "https://example.com")!
    let port = Environment.environment.PORT ?? 80
    

    Types may also be specified explicitly.

    let duration: TimeInterval = Environment.environment.DURATION ?? 1
    

    Declaration

    Swift

    public subscript<T>(dynamicMember member: String) -> T? where T : EnvironmentStringConvertible { get nonmutating set }

    Parameters

    dynamicMember

    The member used to access the environment variable.

    Return Value

    The environment variable value converted to type T using the EnvironmentStringConvertible protocol for the given dynamicMember if dynamicMember is in the environment; otherwise, nil.

  • Accesses the environment variable associated with the given key for reading and writing and converts type T using the EnvironmentStringConvertible protocol.

    The following example gets environment variables of different types from the environment. Default values are provided using the nil-coalescing operator (??) which also enables type inference.

    let url = Environment["SERVER_URL"] ?? URL(string: "https://example.com")!
    let port = Environment["PORT"] ?? 80
    

    Types may also be specified explicitly.

    let duration: TimeInterval = Environment["DURATION"] ?? 1
    

    Declaration

    Swift

    public static subscript<T>(key: String) -> T? where T : EnvironmentStringConvertible { get set }

    Parameters

    key

    The key used to access the environment variable.

    Return Value

    The environment variable value converted to type T using the EnvironmentStringConvertible protocol for the given key if key is in the environment; otherwise, nil.

  • Accesses the environment variable associated with the given member for reading and writing and converts type T using the EnvironmentStringConvertible protocol.

    The following example gets environment variables of different types from the environment. Default values are provided using the nil-coalescing operator (??) which also enables type inference.

    let url = Environment.SERVER_URL ?? URL(string: "https://example.com")!
    let port = Environment.PORT ?? 80
    

    Types may also be specified explicitly.

    let duration: TimeInterval = Environment.DURATION ?? 1
    

    Declaration

    Swift

    public static subscript<T>(dynamicMember member: String) -> T? where T : EnvironmentStringConvertible { get set }

    Parameters

    dynamicMember

    The member used to access the environment variable.

    Return Value

    The environment variable value converted to type T using the EnvironmentStringConvertible protocol for the given dynamicMember if dynamicMember is in the environment; otherwise, nil.