Introduction
Welcome to the documentation for SwiftCSV, a Swift library for working with CSV files. This library provides easy-to-use methods for reading, writing, and manipulating CSV data in your Swift applications.
Features
- Parsing: Parse CSV data into Swift objects
- Writing: Export Swift objects to CSV format
- Data Manipulation: Add, remove, and edit CSV data easily
- Unicode Support: Handles CSV files with international characters
- Custom Delimiters: Use delimiters other than commas for CSV parsing
Requirements
To use SwiftCSV in your project, you will need:
- Xcode 11 or later
- Swift 5.0 or later
- iOS 10.0 or later, macOS 10.10 or later
Installation
To install SwiftCSV using CocoaPods, add the following line to your Podfile
:
pod 'SwiftCSV'
To install SwiftCSV using Swift Package Manager, add the following dependency to your Package.swift
file:
dependencies: [
.package(url: "https://github.com/swiftcsv/SwiftCSV.git", from: "0.6.7")
]
Usage
Once you have installed SwiftCSV in your project, you can start using it. Here are the basic functionalities that SwiftCSV offers:
Reading CSV
To read a CSV file, you need to create an instance of the CSV
class with the file URL:
import SwiftCSV
let url = URL(fileURLWithPath: "/path/to/your/csv/file.csv")
let csv = try! CSV(url: url)
Writing CSV
To write data into a CSV file, you can create a new instance of the CSV
class and set the data:
import SwiftCSV
let csv = CSV()
csv.rows = [
["Name", "Age", "Country"],
["John", "25", "USA"],
["Emily", "21", "UK"]
]
try! csv.write(to: url)
Manipulating CSV
You can easily manipulate CSV data using SwiftCSV’s methods. Here are some examples:
Adding Rows
You can add rows to the CSV using the append
method:
csv.append(row: ["Sarah", "30", "Australia"])
Removing Rows
You can remove rows from the CSV using the remove
method:
csv.remove(rowAtIndex: 2)
Editing Rows
You can edit values in a row using the update
method:
csv.update(rowAtIndex: 1, columnAtIndex: 1, newValue: "22")
Parsing Options
SwiftCSV provides parsing options to customize the way the CSV data is parsed. These options can be passed when creating the CSV
instance:
let csv = try! CSV(url: url, delimiter: ";", encoding: .utf8)
Conclusion
SwiftCSV is a powerful library that makes working with CSV files a breeze. Whether you need to parse CSV data, write CSV files, or manipulate existing ones, SwiftCSV has you covered. Feel free to explore the library further and make the most out of its features in your Swift projects.