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 CSV row as an array of strings.
39
+
A `CSVReadder` parses CSV data from a given input (`String`, or `Data`, or file) and returns CSV rows as a `String`s array. `CSVReader` can be used at a "high-level", in which case it parses an input completely; or at a lower level, in which each row is decoded when requested.
40
+
41
+
- Complete input parsing.
42
+
43
+
```swift
44
+
let file =try CSVReader.parse(input: ...)
45
+
// file is of type: CSVReader.Output
46
+
```
47
+
48
+
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.
40
49
41
50
- Row-by-row parsing.
42
51
43
52
```swift
44
-
let reader =tryCSVReader(data: ...)
53
+
let reader =tryCSVReader(input: "...")
45
54
whilelet row =try reader.parseRow() {
46
55
// Do something with the row: [String]
47
56
}
48
57
```
49
58
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` ismarket with `.firstLine`).
59
+
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` ismarked with `.firstLine`).
51
60
52
61
- `Sequence` syntax parsing.
53
62
54
63
```swift
55
-
let reader =tryCSVReader(fileURL: ...)
64
+
let reader =tryCSVReader(input: URL(...), configuration: ...)
56
65
for row in reader {
57
66
// Do something with the row: [String]
58
67
}
59
68
```
60
69
61
70
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.
62
71
63
-
- Whole input parsing.
64
-
65
-
```swift
66
-
let file =try CSVReader.parse(string: ..., configuration: ...)
67
-
// file is of type: CSVReader.Output
68
-
```
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
-
72
72
### Reader Configuration
73
73
74
74
`CSVReader` accepts the following configuration properties:
@@ -96,7 +96,7 @@ A `CSVReadder` parses CSV data from an input and returns CSV row as an array of
96
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:
97
97
98
98
```swift
99
-
let reader =CSVReader(data: ...) {
99
+
let reader =CSVReader(input: ...) {
100
100
$0.encoding= .utf8
101
101
$0.delimiters.row="\r\n"
102
102
$0.headerStrategy= .firstLine
@@ -108,6 +108,31 @@ let reader = CSVReader(data: ...) {
A `CSVWriter` encodes CSV information into a specified target (i.e. a `String`, or `Data`, or a file). It can be used at a "high-level", by encoding completely a prepared setof information; or at a lower level, in which case rows or fields can be writen individually.
112
+
113
+
- Full encoding.
114
+
115
+
```swift
116
+
let data =try CSVWriter.serialize(rows: [...], into: Data.self)
/// - parameter view: The unicode characters forming a targeted delimiter.
36
+
/// - parameter delimiter: The unicode characters forming a targeted delimiter.
34
37
/// - parameter buffer: A unicode character buffer containing further characters to parse.
35
38
/// - parameter decoder: The instance providing the input `Unicode.Scalar`s.
36
39
/// - returns: A closure which given the targeted unicode character and the buffer and iterrator, returns a Boolean indicating whether there is a delimiter.
Copy file name to clipboardExpand all lines: Sources/Active/Reader/ReaderInternals.swift
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@ extension CSVReader {
2
2
/// Reader status indicating whether there are remaning lines to read, the CSV has been completely parsed, or an error occurred and no further operation shall be performed.
3
3
publicenumStatus{
4
4
/// The CSV file hasn't been completely parsed.
5
-
casereading
5
+
caseactive
6
6
/// There are no more rows to read. The EOF has been reached.
7
7
case finished
8
8
/// An error has occurred and no further operations shall be performed with the reader instance.
0 commit comments