You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`Codable` is just a type alias for `Decodable` and `Encodable`. When a custom type conforms to `Codable`, the type is stating that it has the ability to decode itself from and encode itself to a external representation. Which representation depends on the decoder or encoder chosen. Foundation provides support for [JSON and Property Lists](https://developer.apple.com/documentation/foundation/archives_and_serialization), but the community provide many other formats, such as: [YAML](https://github.com/jpsim/Yams), [XML](https://github.com/MaxDesiatov/XMLCoder), [BSON](https://github.com/OpenKitten/BSON), and CSV (through this library).
363
+
When a custom type conforms to `Codable`, the type is stating that it has the ability to decode itself from and encode itself to a external representation. Which representation depends on the decoder or encoder chosen. Foundation provides support for [JSON and Property Lists](https://developer.apple.com/documentation/foundation/archives_and_serialization), but the community provide many other formats, such as: [YAML](https://github.com/jpsim/Yams), [XML](https://github.com/MaxDesiatov/XMLCoder), [BSON](https://github.com/OpenKitten/BSON), and CSV (through this library).
364
364
365
365
Lets see a regular CSV encoding/decoding usage through `Codable`'s interface. Let's suppose we have a list of students formatted in a CSV file:
366
366
@@ -393,7 +393,7 @@ let students = try decoder.decode([Student], from: data)
393
393
The inverse process (from Swift to CSV) is very similar (and simple).
394
394
395
395
```swift
396
-
let encoder =CSVEncoder { $0.headerStraty=.firstLine }
396
+
let encoder =CSVEncoder { $0.headers=["name", "age", "hasPet"] }
Copy file name to clipboardExpand all lines: sources/Active/Writer/Writer.swift
+10-6Lines changed: 10 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,8 @@ public final class CSVWriter {
22
22
/// The field to write next.
23
23
publicprivate(set)varfieldIndex:Int
24
24
/// The number of fields per row that are expected.
25
+
///
26
+
/// It is zero, if no expectectations have been set.
25
27
private(set)internalvarexpectedFields:Int
26
28
27
29
/// Designated initializer for the CSV writer.
@@ -118,22 +120,24 @@ extension CSVWriter {
118
120
/// It is perfectly fine to call this method when only some fields (but not all) have been writen. This function will complete the row writing row delimiters.
119
121
/// - throws: `CSVError<CSVWriter>` exclusively.
120
122
publicfunc endRow()throws{
123
+
// 1. Has any field being writen for the current row? If not, write a complete emtpy row.
121
124
guardself.fieldIndex >0else{
122
125
returntryself.writeEmptyRow()
123
126
}
124
-
127
+
// 2. If the number of fields per row is known, write the missing fields (if any).
125
128
ifself.expectedFields >0{
126
-
trystride(from:self.fieldIndex, to:self.expectedFields, by:1).forEach{[f =self.settings.delimiters.field] _ in
Copy file name to clipboardExpand all lines: sources/Codable/Encodable/Shadow/ShadowEncoder.swift
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -38,7 +38,7 @@ extension ShadowEncoder {
38
38
do{ // It is weird that this function (as it's defined in the `Encoder` protocol) doesn't throw. Instead there is just a warning in the function documentation.
do{ // It is weird that this function (as it's defined in the `Encoder` protocol) doesn't throw. Instead there is just a warning in the function documentation.
/// Retrieves and removes from the buffer all rows/fields from the given indices.
49
+
///
50
+
/// This function never returns rows at an index smaller than the passed `rowIndex`. Also, for the `rowIndex`, it doesn't return the fields previous the `fieldIndex`.
0 commit comments