SwiftKit/Demo/Shared/Data/StandardCsvParserScreen.swift

61 lines
1.6 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// StandardCsvParserScreen.swift
// Demo
//
// Created by Daniel Saidi on 2020-11-30.
// Copyright © 2020-2022 Daniel Saidi. All rights reserved.
//
import SwiftKit
import SwiftUI
import SwiftUIKit
struct StandardCsvParserScreen: View {
private let parser: CsvParser = StandardCsvParser()
@State private var result = [[String]]()
var body: some View {
List {
Section {
ListText("A CsvParser can parse CSV strings. This demo uses StandardCsvParser to parse a demo file.")
}
ConditionalView(result.hasContent) {
Section(header: Text("Result")) {
ForEach(result, id: \.[0]) { person in
Text("\(person[0]) (age \(person[1]))")
}
}
}
Section(header: Text("Result")) {
ListButton(action: parseFile) {
Label("Parse CSV file", image: .file)
}
}
}.navigationTitle("StandardCsvParser")
}
}
private extension StandardCsvParserScreen {
func parseFile() {
guard
let path = Bundle.main.path(forResource: "Persons", ofType: "csv"),
let data = FileManager.default.contents(atPath: path),
let string = String(data: data, encoding: .utf8)
else { return }
result = parser.parseCsvString(string, componentSeparator: ";")
}
}
struct StandardCsvParserScreen_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
StandardCsvParserScreen()
}
}
}