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 givenkey
ifkey
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 givendynamicMember
ifdynamicMember
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 givenkey
ifkey
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 givendynamicMember
ifdynamicMember
is in the environment; otherwise,nil
.
-
Accesses the environment variable associated with the given key for reading and writing and converts type
T
using theEnvironmentStringConvertible
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 theEnvironmentStringConvertible
protocol for the givenkey
ifkey
is in the environment; otherwise,nil
. -
Accesses the environment variable associated with the given member for reading and writing and converts type
T
using theEnvironmentStringConvertible
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 theEnvironmentStringConvertible
protocol for the givendynamicMember
ifdynamicMember
is in the environment; otherwise,nil
. -
Accesses the environment variable associated with the given key for reading and writing and converts type
T
using theEnvironmentStringConvertible
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 theEnvironmentStringConvertible
protocol for the givenkey
ifkey
is in the environment; otherwise,nil
. -
Accesses the environment variable associated with the given member for reading and writing and converts type
T
using theEnvironmentStringConvertible
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 theEnvironmentStringConvertible
protocol for the givendynamicMember
ifdynamicMember
is in the environment; otherwise,nil
.