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
keyThe key used to access the environment variable.
Return Value
The environment variable value as a
Stringfor the givenkeyifkeyis 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.HOSTDeclaration
Swift
public subscript(dynamicMember member: String) -> String? { get nonmutating set }Parameters
dynamicMemberThe member used to access the environment variable.
Return Value
The environment variable value as a
Stringfor the givendynamicMemberifdynamicMemberis 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
keyThe key used to access the environment variable.
Return Value
The environment variable value as a
Stringfor the givenkeyifkeyis 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.HOSTDeclaration
Swift
public static subscript(dynamicMember member: String) -> String? { get set }Parameters
dynamicMemberThe member used to access the environment variable.
Return Value
The environment variable value as a
Stringfor the givendynamicMemberifdynamicMemberis in the environment; otherwise,nil.
-
Accesses the environment variable associated with the given key for reading and writing and converts type
Tusing theEnvironmentStringConvertibleprotocol.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"] ?? 80Types may also be specified explicitly.
let duration: TimeInterval = Environment.environment["DURATION"] ?? 1Declaration
Swift
public subscript<T>(key: String) -> T? where T : EnvironmentStringConvertible { get nonmutating set }Parameters
keyThe key used to access the environment variable.
Return Value
The environment variable value converted to type
Tusing theEnvironmentStringConvertibleprotocol for the givenkeyifkeyis in the environment; otherwise,nil. -
Accesses the environment variable associated with the given member for reading and writing and converts type
Tusing theEnvironmentStringConvertibleprotocol.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 ?? 80Types may also be specified explicitly.
let duration: TimeInterval = Environment.environment.DURATION ?? 1Declaration
Swift
public subscript<T>(dynamicMember member: String) -> T? where T : EnvironmentStringConvertible { get nonmutating set }Parameters
dynamicMemberThe member used to access the environment variable.
Return Value
The environment variable value converted to type
Tusing theEnvironmentStringConvertibleprotocol for the givendynamicMemberifdynamicMemberis in the environment; otherwise,nil. -
Accesses the environment variable associated with the given key for reading and writing and converts type
Tusing theEnvironmentStringConvertibleprotocol.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"] ?? 80Types may also be specified explicitly.
let duration: TimeInterval = Environment["DURATION"] ?? 1Declaration
Swift
public static subscript<T>(key: String) -> T? where T : EnvironmentStringConvertible { get set }Parameters
keyThe key used to access the environment variable.
Return Value
The environment variable value converted to type
Tusing theEnvironmentStringConvertibleprotocol for the givenkeyifkeyis in the environment; otherwise,nil. -
Accesses the environment variable associated with the given member for reading and writing and converts type
Tusing theEnvironmentStringConvertibleprotocol.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 ?? 80Types may also be specified explicitly.
let duration: TimeInterval = Environment.DURATION ?? 1Declaration
Swift
public static subscript<T>(dynamicMember member: String) -> T? where T : EnvironmentStringConvertible { get set }Parameters
dynamicMemberThe member used to access the environment variable.
Return Value
The environment variable value converted to type
Tusing theEnvironmentStringConvertibleprotocol for the givendynamicMemberifdynamicMemberis in the environment; otherwise,nil.
View on GitHub
Environment Structure Reference