Swiftpack.co - Package - jdfergason/swift-toml

Build Status codebeat badge


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:

  1. Read TOML from file
  2. Parse TOML from string

Both functions return a Toml object of parsed key/value pairs

Parse TOML from file on disk

import Toml
let toml = try Toml(contentsOfFile: "/path/to/file.toml")

Parse TOML from string

import Toml
let toml = try Toml(withString: "answer = 42")

Get raw values from TOML document

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 4.0.3 and TOML 0.4.0.

It has been tested with Swift 4.0.3 on Mac OS X and Ubuntu 16.04.


To run the unit tests checkout the repository and type:

swift test


Apache License 2.0


Stars: 40


Used By

Total: 0




  • Linux support


  • Updated to Swift 4.0.3
  • Significant performance improvements
  • Move equatable == method to a static member to better conform with Swift3 conventions
  • SetValueProtocol method renamed from func setValue(key: , value: ) to func set(value:, for:)
  • Add support for running tests from XCode
  • Updated examples in README.md to reflect current API
  • Update String handling to not use .characters view per Swift4 conventions
  • Use range(at:) instead of rangeAt(_:)



  • Serialize a Toml object to a valid TOML string
  • Resolve the full key path to tables through prefixPath: [String]
  • All accessors have [String] and String... (variadic) based variants


  • All accessor methods now return Optionals and do not throw. Notably, this means that TomlError.KeyError is no longer thrown if a missing key is requested.
  • Toml.keys removed; use: keyNames: Set<[String]> to access key names
  • tableNames: Set<[String]> is now part of the public API for accessing table names
  • Toml.description now returns a valid TOML string
  • Toml.setValue(_:[String]) is now part of the public API
  • Toml.float has been removed



  • hasKey(_ key: [String]) throws -> Bool changed to hasKey(key: [String], includeTables: Bool = true) throws -> Bool to support inclusion of inline tables as valid key paths.
  • hasKey(_ key: ...) throws -> Bool now returns true if the key path refers to a table


  • hasTable(_: [String]) now correctly returns true for inline tables
  • table(_: String...) now correctly returns a Toml table for the requested key path



  • Support for iterating over all tables at a given level with tables(_: [String]) method.
  • Added public API call for retrieving a TOML table at a specified level with table(from: [String]) method.


  • Rename arrayWithPath(keyPath: [String]) to array(_: [String]) for consistency with the rest of the public API.


  • hasTable(_: [String]) now correctly returns true for implicitly defined tables.



  • Updated to Swift 3.0 preview 6


Initial release. For development testing only.