Build Status codebeat badge

SwiftToml

SwiftToml 是一个用 Swift 语言编写的 TOML 解析器。 TOML 是一种直观的配置文件格式,旨在易于人类阅读和计算机解析。

SwiftToml 目前解析符合 TOML 规范 0.4.0 版本的 文件。

有关编写 TOML 文件的完整详细信息,请参阅 TOML 文档

快速入门

TOML 文件通过以下两个函数之一进行解析

  1. 从文件读取 TOML
  2. 从字符串解析 TOML

这两个函数都返回一个包含已解析的键/值对的 Toml 对象

从磁盘上的文件解析 TOML

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

从字符串解析 TOML

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

从 TOML 文档中获取原始值

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") { ... }

安装

将此项目作为依赖项添加到您的 Package.swift 文件中

dependencies: [
    .Package(url: "http://github.com/jdfergason/swift-toml", majorVersion: 1)
]

兼容性

SwiftToml 兼容 Swift 4.0.3 和 TOML 0.4.0。

它已经在 Mac OS X 和 Ubuntu 16.04 上使用 Swift 4.0.3 进行了测试。

测试

要运行单元测试,请检出存储库并键入

swift test

许可证

Apache License 2.0