Swift:
Working with CSV
How to:
In Swift, there isn’t native support for parsing CSV files directly, but you can handle CSV data by using the String
methods to split the contents, or by leveraging third-party libraries such as SwiftCSV for a more streamlined approach. Here are both methods:
Manual Parsing without External Libraries
// Consider a simple CSV string
let csvString = """
name,age,city
John Doe,29,New York
Jane Smith,34,Los Angeles
"""
// Split the CSV string into lines
let rows = csvString.components(separatedBy: "\n")
// Extract the keys from the first row
let keys = rows.first?.components(separatedBy: ",")
// Iterate over the rows starting from the second one
var result: [[String: String]] = []
for row in rows.dropFirst() {
let values = row.components(separatedBy: ",")
let dict = Dictionary(uniqueKeysWithValues: zip(keys!, values))
result.append(dict)
}
// Sample Output
print(result)
// Outputs: [{"city": "New York", "age": "29", "name": "John Doe"}, {"city": "Los Angeles", "age": "34", "name": "Jane Smith"}]
This approach is straightforward but lacks robustness, especially with CSV files containing special cases like commas in values, line breaks within fields, etc.
Using SwiftCSV Library
First, add SwiftCSV to your project by including it in your Package.swift
dependencies:
.package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.5.6")
Then, import and use it as follows:
import SwiftCSV
// Assume `csvString` is defined as above
// Create a CSV object
if let csv = try? CSV(string: csvString) {
// Access rows as dictionaries
let rows = csv.namedRows
// Sample Output
print(rows)
// Outputs: [{"city": "New York", "age": "29", "name": "John Doe"}, {"city": "Los Angeles", "age": "34", "name": "Jane Smith"}]
}
SwiftCSV simplifies parsing by automatically dealing with nuances like encapsulated commas, line breaks in fields, and character encoding. However, remember to handle possible errors in real-world applications, especially when dealing with external data sources.