Working with YAML in Swift (iOS/macOS) — Yams and YamlSwift Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with YAML in Swift (iOS/macOS)



YAML (YAML Ain't Markup Language) is a convenient data serialization format widely used for configuration files, API descriptions, and data exchange. In the Swift ecosystem for iOS and macOS, several reliable libraries are available for parsing and generating YAML. In this article, we will look at the two most popular ones: Yams and YamlSwift. You will learn how to install them in your project, perform basic parsing, serialization, and handle possible errors.



1. Library Overview



Yams

Yams is a modern library for working with YAML, written entirely in Swift. It supports the YAML 1.2 specification and includes a lexer, parser, and serialization tools. Yams is actively maintained by the community and is an excellent choice for projects using Swift Package Manager or CocoaPods.



YamlSwift

YamlSwift is a lighter library, also written entirely in Swift. It provides a minimalistic API for parsing YAML strings and files. YamlSwift is ideal for simple scenarios where full support for all YAML features is not required.



2. Installation



Both libraries can be installed via Swift Package Manager (SPM) or CocoaPods. Let's look at both options.



Installation via Swift Package Manager (recommended)

In Xcode, open File → Add Packages and paste the repository URL:

  • Yams: https://github.com/jpsim/Yams.git
  • YamlSwift: https://github.com/behrang/YamlSwift.git

Select the desired version (the latest stable release is recommended) and add the package to your target.



Installation via CocoaPods

Add the line to your Podfile:

# For Yamspod 'Yams'

# For YamlSwiftpod 'YamlSwift'

Then run pod install.



3. Parsing YAML with Yams



After installation, import the library into your Swift file:

import Yams


Basic String Parsing

Suppose we have a YAML string with user information:

let yamlString = """name: Ivan Petrovage: 30languages:  - Swift  - Python  - JavaScript"""

Parsing is done using the Yams.load(yaml:) function:

do {    if let result = try Yams.load(yaml: yamlString) as? [String: Any] {        print("Name: \\(result["name"] ?? "")")        print("Age: \\(result["age"] ?? 0)")        if let languages = result["languages"] as? [String] {            print("Languages: \\(languages.joined(separator: ", "))")        }    }} catch {    print("YAML parsing error: \\(error.localizedDescription)")}


Parsing from a File

To load YAML from a file in the Bundle, use:

guard let filePath = Bundle.main.path(forResource: "config", ofType: "yaml"),      let yamlString = try? String(contentsOfFile: filePath, encoding: .utf8) else {    fatalError("File config.yaml not found")}

do { let config = try Yams.load(yaml: yamlString) as? [String: Any] // work with config} catch { print("Error: \\(error)")}


4. Serialization (Creating YAML) with Yams



Yams also allows converting Swift objects back into a YAML string. Use the Yams.dump(object:) function:

let dictionary: [String: Any] = [    "name": "Anna Smirnova",    "age": 25,    "skills": ["iOS", "macOS", "watchOS"]]

do { let yamlString = try Yams.dump(object: dictionary) print(yamlString)} catch { print("Serialization error: \\(error)")}

The result will look like this:

age: 25name: Anna Smirnovaskills:-                    

Recommendations