-1

I have the following structure and would like to import the data from a txt file but from a web page instead of the app, is there any way to do it?

I want to access the data through a web address.

if let filePath = Bundle.main.path(forResource: "archivotxt", ofType: "txt") {

        do {
            let contenido = try String(contentsOfFile: filePath)
            let fila = contenido.components(separatedBy: "\n")
            for i in 1..<fila.count {
                let datosArchivo = fila[i].components(separatedBy: "\t")
                //print(datosArchivo[1])
                let campos = Lista(nom: datosArchivo[0], dir: datosArchivo[1], tel: datosArchivo[2])
                self.lista.append(campos)
            }
        } catch let error as NSError {
            print("error al leer el archivo", error)
        }

    }else{
        print("no existe el archivo")
    }
Miquel Molina
  • 23
  • 2
  • 8

1 Answers1

0

I found this, I answer to myself, in case someone is interested

 let datos = "http://misitioweb/archivos/archivotxt.txt"


if let filePath = URL(string: datos) {
    do {
        let contenido = try String(contentsOf: filePath)
        let fila = contenido.components(separatedBy: "\n")
        for i in 1..<fila.count {
            let datosArchivo = fila[i].components(separatedBy: "\t")
            //print(datosArchivo[1])
            let campos = Lista(nom: datosArchivo[0], dir: datosArchivo[1], tel: datosArchivo[2])
            self.lista.append(campos)
        }
    } catch let error as NSError {
        print("error al leer el archivo", error)
    }

}else{
    print("no existe el archivo")
}
Miquel Molina
  • 23
  • 2
  • 8