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 CSV input can be decoded _on demand_ with the decoder's `lazy(from:)` function.
383
383
384
384
```swift
385
-
var sequence = CSVDecoder().lazy(from: fileURL)
385
+
let lazyDecoder = CSVDecoder().lazy(from: fileURL)
386
386
while let row = sequence.next() {
387
387
let student = try row.decode(Student.self)
388
388
// Do something here
389
389
}
390
390
```
391
391
392
-
`LazySequence` conforms to Swift's [`Sequence` protocol](https://developer.apple.com/documentation/swift/sequence), letting you use functionality such as `map()`, `allSatisfy()`, etc. Please note, `LazySequence` cannot be used for repeated access. It _consumes_ the input CSV.
392
+
`LazyDecoder` conforms to Swift's [`Sequence` protocol](https://developer.apple.com/documentation/swift/sequence), letting you use functionality such as `map()`, `allSatisfy()`, etc. Please note, `LazyDecoder` cannot be used for repeated access. It _consumes_ the input CSV.
393
393
394
394
```swift
395
-
var sequence = decoder.lazy(from: fileData)
396
-
let students = try sequence.map { try $0.decode(Student.self) }
395
+
let lazyDecoder = CSVDecoder(configuration: config).lazy(from: fileData)
396
+
let students = try lazyDecoder.map { try $0.decode(Student.self) }
397
397
```
398
398
399
399
A nice benefit of using the _lazy_ operation, is that it lets you switch how a row is decoded at any point. For example:
400
400
```swift
401
-
var sequence = decoder.lazy(from: fileString)
402
-
let students = zip( 0..<100, sequence) { (_, row) in row.decode(Student.self) }
403
-
let teachers = zip(100..<110, sequence) { (_, row) in row.decode(Teacher.self) }
401
+
let lazyDecoder = decoder.lazy(from: fileString)
402
+
let students = ( 0..<100).map { _ in try lazyDecoder.decode(Student.self) }
403
+
let teachers = (100..<110).map { _ in try lazyDecoder.decode(Teacher.self) }
404
404
```
405
405
406
-
Since `LazySequence` exclusively provides sequential access; setting the buffering strategy to `.sequential` will reduce the decoder's memory usage.
406
+
Since `LazyDecoder` exclusively provides sequential access; setting the buffering strategy to `.sequential` will reduce the decoder's memory usage.
407
407
408
408
```swift
409
409
let decoder = CSVDecoder {
@@ -420,7 +420,7 @@ let decoder = CSVDecoder {
420
420
421
421
```swift
422
422
let encoder = CSVEncoder()
423
-
let data: Data = try encoder.encode(value)
423
+
let data= try encoder.encode(value, into: Data.self)
424
424
```
425
425
426
426
The `Encoder`'s `encode()` function creates a CSV file as a `Data` blob, a `String`, or an actual file in the file system.
/// Returns an instance to encode row-by-row the feeded values.
88
+
/// - parameter fileURL: The file receiving the encoded values.
89
+
/// - parameter append: In case an existing file is under the given URL, this Boolean indicates that the information will be appended to the file (`true`), or the file will be overwritten (`false`).
90
+
/// - returns: Instance used for _on demand_ encoding.
0 commit comments