UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

CSV Parser - Problems decoding some strings in my App View

Forums > SwiftUI

How do I decode these strings correctly?

For some reason I can not insert an image in this markdown text to show you the problem.

But for example, words like Securitização de Recebíveis appear Securitiza??o de Receb?veis in my app view.

Please find the code below.

Thanks for the support

import SwiftUI

struct Empresa: Identifiable {

    let id = UUID()
    let DENOM_SOCIAL: String
    let CD_CVM: String
    let SETOR_ATIV: String

    init?(csv: String) {
        let fields = csv.components(separatedBy: ";")
        guard fields.count > 4 else { return nil }
        self.CD_CVM = fields[9]
        self.DENOM_SOCIAL = fields[1]
        self.SETOR_ATIV = fields[10]
    }
}

struct ContentView: View {
    @State private var empresas = [Empresa]()

    var body: some View {
        List(empresas) { empresa in
            VStack(alignment: .leading) {
                Text("\(empresa.CD_CVM) - \(empresa.DENOM_SOCIAL)")
                    .font(.headline)
                Text(empresa.SETOR_ATIV)
            }
        }
        .task {
            do {
                let url = URL(string: "http://dados.cvm.gov.br/dados/CIA_ABERTA/CAD/DADOS/cad_cia_aberta.csv")!
                let empresaData = url.lines.compactMap(Empresa.init)

                for try await empresa in empresaData {
                    empresas.append(empresa)
                }
            } catch {
                // Stop adding empresas when an error is thrown
            }
        }
    }
}

3      

What encoding does your file use? Is it UTF-8 or something different?

3      

I guess iso-8859-1...the words are in portuguese.

3      

I found a solution but it's not as neat as the url.lines. And it doesn't load the lines one by one and your UI doesn't respond.

let (data, ) = try await URLSession.shared.data(from: url)
let strings = String(data: data, encoding: .isoLatin1)!
let parsedCSV: [String] = strings.components(separatedBy: "\n")
for parsed in parsedCSV {
    if let e = Empresa(csv: parsed) {
        empresas.append(e)
    }
}

3      

ok, thanks a lot.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.