SwiftToml is a TOML parser written in the swift language. TOML is an intuitive configuration file format that is designed to be easy for humans to read and computers to parse.
SwiftToml currently parses files that conform to version 0.4.0 of the TOML spec.
For full details of writing TOML files see the TOML documentation.
TOML files are parsed using one of two functions:
- Read TOML from file
- Parse TOML from string
Both functions return a Toml object of parsed key/value pairs
import Toml
let toml = try Toml(contentsOfFile: "/path/to/file.toml")
import Toml
let toml = try Toml(withString: "answer = 42")
import Toml
let toml = try Toml(contentsOfFile: "/path/to/file.toml")
// string value
print(toml.string("table1", "name"))
// boolean value
print(toml.bool("table1", "manager"))
// integer value
print(toml.int("table1", "age"))
// double value
print(toml.double("table1", "rating"))
// date value
print(toml.date("table1", "birthday"))
// get value and resolve type at runtime
print(try toml.value("title")!)
// get array of type [String]
let array: [String] = toml.array("locations")!
// get table
let table1 = toml.table("table1")
// iterate over all tables at the root level
for (tablePath, table) in toml.tables() { ... }
// iterate over all tables under table1
for (tablePath, table) in toml.tables("table1") { ... }
Add the project to to your Package.swift file as a dependency:
dependencies: [
.Package(url: "http://github.com/jdfergason/swift-toml", majorVersion: 1)
]
SwiftToml is compatible with Swift 3.0 and TOML 0.4.0.
It has been tested with Swift 3.0 GM Candidate on Mac OS X, Ubuntu 14.04, and Fedora 23. Currently it only works with Mac OS X due to some features of Swift not currently working under linux. I am actively working to fully support linux.
To run the unit tests checkout the repository and type:
swift test