SwiftConfig

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

SwiftConfig: Managing Application Configuration in Swift

SwiftConfig is a lightweight library for Swift (iOS/macOS) designed for centralized management of application settings. It allows you to load, parse, and access configuration data from JSON, YAML, or plist files, and also supports environment variables. The library is especially useful for projects that require flexible configuration of application behavior without recompilation (for example, switching between staging/production servers).

Why is SwiftConfig needed?

In standard iOS/macOS development, UserDefaults, Info.plist, or hardcoded constants are often used. This leads to problems:

  • Mixing code and configuration
  • Lack of typing (all values are strings)
  • Difficulty switching between environments
SwiftConfig solves these problems by providing a strictly typed API and support for multiple data sources.

Installation

Add SwiftConfig via Swift Package Manager in Xcode:

// Package.swift or via Xcode menu: File > Add Packages....package(url: "https://github.com/example/SwiftConfig", from: "1.0.0")

Or via CocoaPods:

pod 'SwiftConfig', '~> 1.0'

Main Features

  • Format support: JSON, YAML, plist, .env
  • Typed access: automatic conversion to Int, Bool, URL, Date
  • Hierarchical keys: access to nested fields via dot notation (e.g., "database.host")
  • Environment variables: env variables take priority over file settings
  • Caching: single load with the ability to force refresh

Code Example

import SwiftConfig

// Define the configuration structurestruct AppConfig: Codable { let apiURL: URL let timeout: Double let features: [String: Bool]}

// Load from config.json filelet config = try SwiftConfig.load( from: "config", // file name without extension type: .json, as: AppConfig.self)

// Usageprint(config.apiURL) // https://api.example.comprint(config.timeout) // 30.0print(config.features["darkMode"]) // true

// Access nested fields without decodinglet host: String = try SwiftConfig.value(for: "database.host")print(host) // localhost

When to use SwiftConfig?

It is recommended for use in the following scenarios:

  • Applications with multiple environments (development, staging, production)
  • Projects where settings change frequently (A/B tests, feature flags)
  • Team development (single configuration file in the repository)
  • Console utilities or server-side Swift applications (Vapor, Kitura)

The library is not suitable for storing user data (use UserDefaults or CoreData).

Conclusion

SwiftConfig simplifies configuration management, making code cleaner and more maintainable. Thanks to strict typing, you avoid runtime errors, and support for multiple formats allows you to easily integrate the library into an existing project.

Recommendations