Classes
The following classes are available globally.
-
Encodes Swift types to CSV data.
This exampls shows how multiple instances of a
Person
type will be encoded to CSV data.Person
conforms toCodable
, so it is compatible with both theCSVEndocder
andCSVDecoder
.
See morestruct Person: Codable { let firstName: String, let lastName: String, let age: Int } let people = [ Person(firstName: "Grace", lastName: "Hopper", age: 113), Person(firstName: "Linus", lastName: "Torvold", age: 50) ] let data = try CSVEncoder().sync.encode(people) print(String(decoding: data, as: UTF8.self)) /* Prints: "firstName","lastName","age" "Grace","Hopper","113" "Linus","Torvold","50" */
Declaration
Swift
public final class CSVEncoder
-
The encoder for encoding multiple objects at once into a single CSV document.
You can get an instance of the
See moreCSVSyncEncoder
with theCSVEncoder.sync
property.Declaration
Swift
public final class CSVSyncEncoder
-
An encoder for encoding multiple objects separately into a single CSV document.
You can get an instance of the
See moreCSVAsyncEncoder
using theCSVEncoder.async(_:)
method.Declaration
Swift
public final class CSVAsyncEncoder
-
Decodes CSV document data to Swift types.
This example shows how a simple
Person
type will be decoded from a CSV document. Personconforms to
Codable, so it is compatible with both the
CSVEndocderand
CSVDecoder`.
See morestruct Person: Codable { let firstName: String, let lastName: String, let age: Int } let csv = """ "firstName","lastName","age" "Grace","Hopper","113" "Linus","Torvold","50" """ let data = Data(csv.utf8) let people = try CSVDecoder.sync.decode(Person.self, from: data) print(people.map { $0.firstName }) // Prints: `["Grace","Linus"]`
Declaration
Swift
public final class CSVDecoder
-
A decoder for decoding a single CSV document all at once.
You can get an instance of
See moreCSVSyncDecoder
from theCSVDecoder.sync
property.Declaration
Swift
public final class CSVSyncDecoder
-
A decoder for decoding sections of a CSV document at different times.
You can get an instance of
See moreCSVAsyncDecoder
from theCSVDecoder.async(for:length_:)
method.Declaration
Swift
public final class CSVAsyncDecoder
-
The options used for encoding/decoding certin types in the
See moreCSVEncoder
andCSVDecoder
.Declaration
Swift
public final class CSVCodingOptions