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
A `CSVReadder` parses CSV data from an input and returns you each CSV row as an array of strings.
39
+
A `CSVReadder` parses CSV data from an input and returns CSV row as an array of strings.
40
40
41
41
- Row-by-row parsing.
42
42
@@ -47,6 +47,8 @@ A `CSVReadder` parses CSV data from an input and returns you each CSV row as an
47
47
}
48
48
```
49
49
50
+
Alternatively you can use the `parseRecord()` function which also returns the next CSV row, but it wraps the result in a convenience structure. This structure lets you access each field with the header name (as long as the `headerStrategy` is market with `.firstLine`).
51
+
50
52
- `Sequence` syntax parsing.
51
53
52
54
```swift
@@ -56,15 +58,17 @@ A `CSVReadder` parses CSV data from an input and returns you each CSV row as an
56
58
}
57
59
```
58
60
59
-
Please note the `Sequence` syntax (i.e. `IteratorProtocol`) doesn't throw errors; therefore if the CSV data is invalid, the previous code will crash your program. If you don't control the origin of the CSV data, use the `parseRow()` function instead.
61
+
Please note the `Sequence` syntax (i.e. `IteratorProtocol`) doesn't throw errors; therefore if the CSV data is invalid, the previous code will crash. If you don't control the CSV data origin, use `parseRow()` instead.
60
62
61
63
- Whole input parsing.
62
64
63
65
```swift
64
66
let file =try CSVReader.parse(string: ..., configuration: ...)
65
-
// file is of type: (headers: [String], rows: [[String]])
67
+
// file is of type: CSVReader.Output
66
68
```
67
69
70
+
This type of parsing returns a simple structure containing the CSV headers and CSV rows. Additionally it lets you access each field through the header name or the field index.
71
+
68
72
### Reader Configuration
69
73
70
74
`CSVReader` accepts the following configuration properties:
@@ -89,7 +93,7 @@ A `CSVReadder` parses CSV data from an input and returns you each CSV row as an
89
93
90
94
Loading all data into memory may provide faster iteration for small to medium size files, since you get rid of the overhead of managing an `InputStream`.
91
95
92
-
The configuration values are only set during initialization and can be passed to the `CSVReader` instance through a structure or with a convenience closure syntax:
96
+
The configuration values are set during initialization and can be passed to the `CSVReader` instance through a structure or with a convenience closure syntax:
93
97
94
98
```swift
95
99
let reader =CSVReader(data: ...) {
@@ -109,7 +113,7 @@ let reader = CSVReader(data: ...) {
109
113
</p></details>
110
114
</ul>
111
115
112
-
## Swift's `Codable`
116
+
## `Codable`'s Decoder/Encoder
113
117
114
118
The encoders/decoders provided by this library let you use Swift's `Codable` declarative approach to encode/decode CSV data.
115
119
@@ -234,12 +238,14 @@ struct Student: Codable {
234
238
}
235
239
```
236
240
241
+
> Using integer coding keys has the added benefit of better encoder/decoder performance. By explicitly indicating the field index, you let the decoder skip the functionality of matching coding keys string values to headers.
242
+
237
243
</p></details>
238
244
<details><summary>A CSV is a long list of records/rows.</summary><p>
239
245
240
246
CSV formatted data is commonly used with flat hierarchies (e.g. a list of students, a list of car models, etc.). Nested structures, such as the ones found in JSON files, are not supported by defaultin CSV implementations (e.g. a list of users, whereeach user has a list of services she uses, and each service has a list of the user's configuration values).
241
247
242
-
You can definitely support complex structures in CSV, but you would have to flatten the hierarchy in a single model or build a custom encoding/decoding process. This process would make sure there is always a maximum of two keyed/unkeyed containers.
248
+
You can support complex structures in CSV, but you would have to flatten the hierarchy in a single model or build a custom encoding/decoding process. This process would make sure there is always a maximum of two keyed/unkeyed containers.
243
249
244
250
As an example, we can create a nested structure for a school with students who own pets.
0 commit comments