Swift 5 library for accessing environment variables from .env files
- Swift 100%
| .github/workflows | ||
| .swiftpm/xcode | ||
| Sources/SwiftDotEnv | ||
| Tests/SwiftDotEnvTests | ||
| .gitignore | ||
| Package.swift | ||
| README.md | ||
SwiftDotEnv
Swift 5 library for accessing environment variables from .env files
Installation
Install using Swift Package Manager.
Add Dependency:
.package(name: "SwiftDotEnv", url: "https://github.com/noahkamara/swiftdotenv", from: "1.0.0")
Usage
import SwiftDotEnv
let envPath = ".env" // or absolute path
let env = DotEnv(withFile: envPath)
// Retrieve variable 'VAR' and default to "DEFAULTVALUE"
let var = env.value("VAR", "DEFAULTVALUE")
Getter Methods:
value(_ key: String, _ default: String? = nil) -> String?
Returns the value for
keyin the environment, returningdefaultif not present
- Parameter
key: Variable key- Parameter
default: Default value
int(for key: String, _ default: Int? = nil) -> Int?
Returns the integer value for
keyin the environment, returningdefaultif not present
- Parameter
key: Variable key- Parameter
default: Default value
bool(for key: String, _ default: Bool? = nil) -> Bool?
Returns the boolean value for
keyin the environment, returningdefaultif not present
- Parameter
key: Variable key- Parameter
default: Default value
Subscript Access:
You can also access variables by subscript (this will return a string!)
let var = env["VAR"]
The .env-file
This is an example for a .env file and also all supported types:
# COMMENT
STRING=ThisIsAString # Inline Comment
STRING_QUOTMARK="String with"
INT=69
BOOL_TRUE=true
BOOL_TRUE_INT=1
BOOL_TRUE_STR=yes
BOOL_FALSE=false
BOOL_FALSE_INT=0
BOOL_FALSE_STR=no
Support for Values-Types
Comments
# This is a comment
KEY=VALUE
Inline Comments
KEY=VALUE # Inline Comment
Strings
STRING=ThisIsASupportedString
QUOTES="This is also a supported String"
Integers
INTEGER=42
Booleans
# Will be evaluated as true:
BOOL1=true
BOOL2=1
BOOL3=yes
# Will be evaluated as false
BOOL4=false
BOOL5=0
BOOL6=no