Swift Package Index


Use INI config files with Configuration!


  • The latest stable release is 0.0.1. Released 2 years ago.
  • The last commit to master was 2 years ago.

Swift Version Compatibility

  • 0.0.1 and master
    5.3
    5.2
    5.1
    5.0
    4.2
Full build results

Platform Compatibility

  • 0.0.1 and master
    iOS
    macOS(Intel)
    macOS(ARM)
    Linux
    tvOS
    watchOS
Full build results

Configuration-INIDeserializer

Like Configuration, but don't think JSON or Plist-flavored XML are good markup languages for writing human-friendly configuration files? I agree. Let's use INI files instead.

Unlike JSON, INI files support comments, and have a far lower "noise" level than both XML and JSON.

Example

Let's write an INI file that looks like this.

; Configuration file for my great Swift app
; Lines that start with the ; character are comments

; Site name
site-name = My Great Site

; Site root URL
site-url = https://example.com/mygreatsite

; Database credentials and configuration
[database]
username = hello
password = world

Now in your Swift project, add Configuration-INIDeserializer using SPM. Once done, here's how you can use it in your code.

import Configuration
import Configuration_INIDeserializer // <= Note the underscore

let config = Configuration()

// Tell Configuration how to read INI files. Don't forget this line!
config.use(INIDeserializer())

// Now you can load an INI file
config.load(file: "/path/to/my-config.conf")

// For top-level values, cast from the top level of the configuration "array"
let siteName = config["site-name"] as? String ?? "Default Site Name"

// For values in sections, try casting the section as a [String: String]
guard let databaseCredentials = config["database"] as? [String: String] else {
    print("Can't find database credentials")
    exit(1)
}
let databaseUsername = databaseCredentials["username"]

Yet Another Swift Project by Nocturnal

Feedback, bug reports, and Patreon tips accepted with a smile!