Working with CSV in Swift (iOS/macOS): SwiftCSV Library and Parsing Basics
CSV (Comma-Separated Values) is one of the most popular formats for storing tabular data. In the Apple ecosystem (iOS, macOS), working with CSV files is often required for importing data from external sources, exporting reports, or loading configurations. In this article, we will look at the SwiftCSV library — a lightweight and convenient solution for parsing CSV in Swift, as well as basic examples of working with this format without third-party dependencies.
What is SwiftCSV?
SwiftCSV is an open-source library for Swift that allows you to easily read and process CSV files. It supports both string parsing and file handling, automatically detects delimiters (comma, tab, semicolon), and provides a convenient API for accessing rows, columns, and individual cells.
Key features:
- Parsing CSV from a string, Data, or URL (local or remote).
- Support for column headers (first row as keys).
- Handling nested quotes and escaped characters.
- Ability to choose a delimiter (comma, tab, custom).
- Minimal dependencies (pure Swift).
Installing SwiftCSV
Add the library via Swift Package Manager (SPM):
- In Xcode, select
File → Add Packages.... - Paste the repository URL:
https://github.com/swiftcsv/SwiftCSV.git. - Choose a version (latest stable recommended).
- Add the package to your target.
Or add it to Package.swift:
dependencies: [ .package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.9.0")]Basic example: reading CSV from a string
Suppose we have a CSV string with user data:
let csvString = """name,age,cityAlice,30,New YorkBob,25,Los AngelesCharlie,35,Chicago"""Parse it using SwiftCSV:
import SwiftCSV
do { let csv = try CSV(string: csvString) // Access rows as dictionaries (keys are headers) for row in csv.rows { if let name = row["name"], let age = row["age"], let city = row["city"] { print("\\(name) (\\(age) years old) lives in \\(city)") } }} catch { print("Parsing error: \\(error)")}Output:
Alice (30 years old) lives in New YorkBob (25 years old) lives in Los AngelesCharlie (35 years old) lives in ChicagoReading CSV from a file (iOS/macOS)
To load CSV from a local file (e.g., in the Documents folder or from the bundle):
import SwiftCSV
// Path to the file (e.g., in Documents)let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("data.csv")
do { let csv = try CSV(url: fileURL) // Access columns let names = csv.columns["name"] ?? [] print("All names: \\(names)")} catch { print("Error: \\(error)")}For a file from the bundle:
if let path = Bundle.main.path(forResource: "data", ofType: "csv") { let url = URL(fileURLWithPath: path) let csv = try CSV(url: url) // Work with data}Custom delimiters and settings
If your CSV uses tabs or semicolons, specify the delimiter explicitly:
// TSV (Tab-Separated Values)let tsv = try CSV(string: "name\\tageAlice\\t30", delimiter: .tab)
// Delimiter — semicolonlet csvSemicolon = try CSV(string: "name;ageAlice;30", delimiter: Character(";"))You can also disable header parsing (the first row will be treated as a regular row):
let csvNoHead