目录
目录README.md

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!

关于
36.0 KB
邀请码
    Gitlink(确实开源)
  • 加入我们
  • 官网邮箱:gitlink@ccf.org.cn
  • QQ群
  • QQ群
  • 公众号
  • 公众号

©Copyright 2023 CCF 开源发展委员会
Powered by Trustie& IntelliDE 京ICP备13000930号