To use it, you should import it using the following statement:
import Yaml
Yaml
A Yaml value can be any of these cases:
enum Yaml {
case null
case bool(Bool)
case int(Int)
case double(Double)
case string(String)
case array([Yaml])
case dictionary([Yaml: Yaml])
}
Yaml.load
Yaml.load (String) throws -> Yaml
Takes a string of a YAML document and returns a Yaml enum.
let value = try! Yaml.load("a: 1\nb: 2")
print(value["a"]) // Int(1)
print(value["b"]) // Int(2)
print(value["c"]) // Null
If the input document is invalid or contains more than one YAML document, an error is thrown.
do {
let value = try Yaml.load("a\nb: 2")
}
catch {
print(error) // expected end, near "b: 2"
}
Yaml.loadMultiple
Yaml.loadMultiple (String) throws -> [Yaml]
Takes a string of one or more YAML documents and returns [Yaml].
let value = try! Yaml.loadMultiple("---\na: 1\nb: 2\n---\na: 3\nb: 4")
print(value[0]["a"]) // Int(1)
print(value[1]["a"]) // Int(3)
It will throw an error if an error is encountered in any of the documents.
Yaml#[Int] -> Yaml
value[Int] -> Yaml
value[Int] = Yaml
If used on a Yaml.array value, it will return the value at the specified index. If the index is invalid or the value is not a Yaml.array, Yaml.null is returned. You can also set a value at a specific index. Enough elements will be added to the wrapped array to set the specified index. If the value is not a Yaml.array, it will change to it after set.
If used on a Yaml.dictionary value, it will return the value for the specified key. If a value for the specified key does not exist, or value is not a Yaml.dictionary, Yaml.null is returned. You can also set a value for a specific key. If the value is not a Yaml.dictionary, it will change to it after set.
Since Yaml is a literal convertible type, you can pass simple values to this method.
Returns an Optional<Dictionary<Yaml, Yaml>> value. If the value is a Yaml.dictionary value, the wrapped value is returned. Otherwise nil is returned.
let value = try! Yaml.load("- Swift: true\n- Objective C: false")
print(value.dictionary) // nil
print(value[0].dictionary) // Optional([String(Swift): Bool(true)])
Yaml#count
value.count -> Int?
Returns an Optional<Int> value. If the value is either a Yaml.array or a Yaml.dictionary value, the count of elements is returned. Otherwise nil is returned.
let value = try! Yaml.load("- Swift: true\n- Objective C: false")
print(value.count) // Optional(2)
print(value[0].count) // Optional(1)
print(value[0]["Swift"].count) // nil
YamlSwift
Load YAML and JSON documents using Swift.
YamlSwift
parses a string of YAML document(s) (or a JSON document) and returns aYaml
enum value representing that string.Install
Use Carthage to build and install.
Or use CocoaPods : Add
pod 'Yaml'
to yourPodfile
and runpod install
.It supports Swift Package Manager.
And:
API
import
To use it, you should import it using the following statement:
Yaml
A Yaml value can be any of these cases:
Yaml.load
Takes a string of a YAML document and returns a
Yaml
enum.If the input document is invalid or contains more than one YAML document, an error is thrown.
Yaml.loadMultiple
Takes a string of one or more YAML documents and returns
[Yaml]
.It will throw an error if an error is encountered in any of the documents.
Yaml#[Int] -> Yaml
If used on a
Yaml.array
value, it will return the value at the specified index. If the index is invalid or the value is not aYaml.array
,Yaml.null
is returned. You can also set a value at a specific index. Enough elements will be added to the wrapped array to set the specified index. If the value is not aYaml.array
, it will change to it after set.Yaml#[Yaml] -> Yaml
If used on a
Yaml.dictionary
value, it will return the value for the specified key. If a value for the specified key does not exist, or value is not aYaml.dictionary
,Yaml.null
is returned. You can also set a value for a specific key. If the value is not aYaml.dictionary
, it will change to it after set.Since
Yaml
is a literal convertible type, you can pass simple values to this method.Yaml#bool
Returns an
Optional<Bool>
value. If the value is aYaml.bool
value, the wrapped value is returned. Otherwisenil
is returned.Yaml#int
Returns an
Optional<Int>
value. If the value is aYaml.int
value, the wrapped value is returned. Otherwisenil
is returned.Yaml#double
Returns an
Optional<Double>
value. If the value is aYaml.double
value, the wrapped value is returned. Otherwisenil
is returned.Yaml#string
Returns an
Optional<String>
value. If the value is aYaml.string
value, the wrapped value is returned. Otherwisenil
is returned.Yaml#array
Returns an
Optional<Array<Yaml>>
value. If the value is aYaml.array
value, the wrapped value is returned. Otherwisenil
is returned.Yaml#dictionary
Returns an
Optional<Dictionary<Yaml, Yaml>>
value. If the value is aYaml.dictionary
value, the wrapped value is returned. Otherwisenil
is returned.Yaml#count
Returns an
Optional<Int>
value. If the value is either aYaml.array
or aYaml.dictionary
value, the count of elements is returned. Otherwisenil
is returned.License
MIT