What is SwiftyConfig?
SwiftyConfig is a lightweight library for managing configurations of iOS/macOS applications in Swift. It provides a convenient interface for reading settings from .plist, .json, or .yaml files, and also supports working with environment variables. The library automatically checks data types, supports nested structures, and allows setting default values.
Why do you need SwiftyConfig?
During application development, there is often a need to store environment settings (API keys, server URLs, feature flags) separately from the code. SwiftyConfig solves this problem by offering:
- Type safety — the compiler checks configuration types at build time.
- Flexibility — support for multiple file formats and data sources.
- Simplicity — minimal code for initialization and access to settings.
Installation
Add to Package.swift:
.package(url: "https://github.com/example/SwiftyConfig.git", from: "1.0.0")Or via CocoaPods:
pod 'SwiftyConfig'Main Features
- Automatic configuration detection — searches for
Config.plistfiles in the bundle. - Support for nested keys — access via dot notation:
config["server.host"]. - Typed getters —
string(forKey:),int(forKey:),bool(forKey:), and others. - Default values — if a key is missing, a specified value is returned.
- Caching — single file read with subsequent in-memory caching.
Code Example
import SwiftyConfig
// Initialization (automatically loads Config.plist)let config = try SwiftyConfig()
// Reading valueslet apiKey: String = config.string(forKey: "api.key") ?? ""let maxRetries: Int = config.int(forKey: "network.maxRetries") ?? 3let isDebugMode: Bool = config.bool(forKey: "debug.enabled") ?? false
// Or with dot notationif let host = config["server.host"] as? String { print("Server host: \\(host)")}
// For macOS, you can add environment variable support:let envConfig = SwiftyConfig(environment: ProcessInfo.processInfo.environment)print(envConfig.string(forKey: "DATABASE_URL") ?? "default")When to use SwiftyConfig?
The library is ideal for:
- Multi-environment applications — dev/staging/prod with different configurations.
- Feature toggles — quickly enabling/disabling features without rebuilding.
- Multi-target builds — iOS, macOS, watchOS with shared settings.
- Open-source projects — where you need to hide real keys and move them to a separate file.
SwiftyConfig is a minimalistic yet powerful solution for configuring Swift applications that simplifies code maintenance and scaling.