A really lightweight
library to easily export any array or dictionary
to a CSV spreadsheet.
It's also customizable
and support Swift 5.1
!
- Installation
- Make your class and struct compatible
- Generate a CSV spreadsheet from any array or dictionary
- Custom the CSV Generator
.package(url: "https://github.com/RomainPct/CSVGeneratorSwift", from: "1.0.1")
github "RomainPct/CSVGeneratorSwift"
Make any Struct or Class compatible with CSVGeneratorSwift by implementing the lightweight CSVExportable protocol.
Just define a varget called CSVFields which return an array of the values you want in your output CSV file.
Order values according to the output organization you want.
struct Car : CSVExportable {
var CSVFields: [Any] {
return [name,dimensions,engine]
}
let name:String
let dimensions:[Int]
let engine:Engine
}
If you use a subobject, make it also conform to CSVExportable protocol.
In this exemple, the Car struct has a variable of type Engine, therefore we make Engine struct conform to CSVExportable protocole.
struct Engine : CSVExportable {
var CSVFields: [Any] {
return [type,autonomy]
}
enum EngineType : String {
case electric
case hybrid
case combustion
}
let type:EngineType
let autonomy:Int // Kilometers
}
- Create a CSVGeneratorSwift instance
- Call the generate function passing your array/dictionary as first parameter
- Pass an optional file name to the generate function Default is your struct/class name with a "s" (ex : The output of an array of Car instances is called "Cars")
- Read the result (which is a Swift.Result that help you to ensure a clean and readable code).
- If the operation succeed, do what you want with the file url (share it with the user, save it somewhere...)
- Else, handle the error
let cars:[Car] = [
Car(name: "Taycan", dimensions: [4684,1923,1624], engine: Engine(type: .electric, autonomy: 500)),
Car(name: "C-HR", dimensions: [4360,1795,1565], engine: Engine(type: .hybrid, autonomy: 1132)),
Car(name: "Model X", dimensions: [5052,1999,1684], engine: Engine(type: .electric, autonomy: 565))
]
let generator = CSVGeneratorSwift()
let csvResult = generator.generate(from: cars, name: "My cool cars !")
switch csvResult {
case .success(let url):
print("Do what you want with the CSV file url : \(url)")
case .failure(let error):
print("CSV generation failed : \(error.localizedDescription)")
}
Choose the destination you want for the CSV File.
Default is temporary directory.
generator.destination = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
Each String.Encoding case is avalaible.
Default is .utf8.
generator.encoding = .utf16
Edit the CSV format easily.
Default values are the more common patterns for CSV : a comma (,) as column separator and a line break (\n) as line end symbol.
generator.columnSeparator = ";"
generator.lineEnd = "\n"
Each JSONSerialization.WritingOptions case is avalaible. It's usefull if the object you whant to insert in your spreadsheet contains array or dictionnaries variables.\ Default is .prettyPrinted.
generator.jsonWritingOptions = .fragmentsAllowed
Customize the output for Date object you insert as CSVField.
Default is "yyyy-MM-dd HH:mm:ss +zzzz".
generator.dateFormat = "dd-MM-YYYY"