split function on a label in Swift 4

Asked

Viewed 41 times

0

Good evening, I am receiving a json from a webservice and in a given field json passes me the data separated by "|" as you can see below

imagem com os |

i need to break the line in each | of the json field. I managed to use the split to print on the separate console but I can’t move to the tableview to do the same.

    import Foundation

struct GUIAmedicos: Codable{
    var ID = ""
    var NOME = ""
    var CONSELHO = ""
    var ESPECIALIDADE = ""
    var RQE = ""
    var CORPOCLINICO = ""
    var CNES = ""
    var URL_CNES = ""
    var CIDADE = ""
    var ENDERECO = ""
    var ENDERECOPRINCIPAL = ""
    var FONE = ""
    var MAPS = ""
    var CNPJ = ""
    var SITE = ""
    var ID_CLASSIFICACAO = ""
    var DATA_DESCREDENCIAMENTO = ""
}

@IBOutlet weak var myTableView: UITableView!


var tableViewDataSource = [GUIAmedicos]()
var filteredMed = [GUIAmedicos]()

var searchController = UISearchController(searchResultsController: nil)

override func viewDidLoad() {
    super.viewDidLoad()

    myTableView.estimatedRowHeight = 150
    myTableView.rowHeight = UITableViewAutomaticDimension
    myTableView.tableFooterView = UIView()


    searchController.searchResultsUpdater = self
    searchController.dimsBackgroundDuringPresentation = false
    definesPresentationContext = true
    myTableView.tableHeaderView = searchController.searchBar
    searchController.searchBar.scopeButtonTitles = ["MEDICO", "ESPECIALIDADE", "CIDADE"]
    searchController.searchBar.delegate = self
    searchController.searchBar.placeholder = "Buscar"

    myTableView.dataSource = self
    myTableView.delegate = self

    loadList()

}


func loadList(){

    var myGuia = GUIAmedicos()
    let task = URLSession.shared.dataTask(with: url!) {
        (data, response, error) in
        if (error != nil) {
            print("error")
        } else {
            do {
                if let content = data {
                    let myJson = try JSONSerialization.jsonObject(with: content, options: .mutableContainers)
                     //print(myJson)

                    if let jsonData = myJson as? [String : Any] {
                        if let myMed = jsonData["medico"] as? [[String : Any]]{
                            //dump(myMed)

                            for value in myMed {
                                if let medNome = value["NOME"] as? String {
                                    //print(medNome)
                                    myGuia.NOME = medNome
                                }
                                if let medConselho = value["CONSELHO"] as? String {
                                    //print(medConselho)
                                    myGuia.CONSELHO = medConselho
                                }
                                if let medEspecialidade = value["ESPECIALIDADE"] as? String {
                                    //print(medEspecialidade)
                                    myGuia.ESPECIALIDADE = medEspecialidade
                                }
                                if let medRqe = value["RQE"] as? String {
                                    //print(medRqe)
                                    myGuia.RQE = medRqe
                                }
                                if let medCorpoClinico = value["CORPOCLINICO"] as? String {
                                    //print(medCorpoClinico)
                                    myGuia.CORPOCLINICO = medCorpoClinico
                                }
                                if let medCnes = value["CNES"] as? String {
                                    //print(medCnes)
                                    myGuia.CNES = medCnes
                                }
                                if let medUrl_Cnes = value["URL_CNES"] as? String {
                                    //print(medUrl_Cnes)
                                    myGuia.URL_CNES = medUrl_Cnes
                                }
                                if let medCidade = value["CIDADE"] as? String {
                                    //print(medCidade)
                                    myGuia.CIDADE = medCidade
                                }
                                if let medEndereco = value["ENDERECO"] as? String {
                                    //print(medEndereco)
                                    myGuia.ENDERECO = medEndereco
                                }
                                if let medEnderecoPrincipal = value["ENDERECOPRINCIPAL"] as? String {
                                    //print(medEnderecoPrincipal)
                                    myGuia.ENDERECOPRINCIPAL = medEnderecoPrincipal
                                }
                                if let medFone = value["FONE"] as? String {
                                    //print(medFone)
                                    myGuia.FONE = medFone
                                }
                                if let medMaps = value["MAPS"] as? String {
                                    //print(medMaps)
                                    myGuia.MAPS = medMaps
                                }
                                if let medCnpj = value["CNPJ"] as? String {
                                    //print(medCnpj)
                                    myGuia.CNPJ = medCnpj
                                }
                                if let medSite = value["SITE"] as? String {
                                    //print(medSite)
                                    myGuia.SITE = medSite
                                }
                                if let medid_classificacao = value["ID_CLASSIFICACAO"] as? String {
                                    //print(medid_classificacao)
                                    myGuia.ID_CLASSIFICACAO = medid_classificacao
                                }
                                self.tableViewDataSource.append(myGuia)
                            }
                            //dump(self.tableViewDataSource)
                            DispatchQueue.main.async {
                                self.myTableView.reloadData()
                            }
                        }
                    }
                }
            } catch {

            }
        }
    }
    task.resume()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    if isFiltering() {
        return filteredMed.count
    }
    return tableViewDataSource.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let myCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)

    if isFiltering() {

        let mNome = myCell.viewWithTag(11) as! UILabel
        let mEspecialidade = myCell.viewWithTag(12) as! UILabel
        let mCorpoClinico = myCell.viewWithTag(13) as! UILabel
        let mCidade = myCell.viewWithTag(14) as! UILabel

        mNome.text = filteredMed[indexPath.row].NOME
        mEspecialidade.text = filteredMed[indexPath.row].ESPECIALIDADE
        mCorpoClinico.text = filteredMed[indexPath.row].CORPOCLINICO
        mCidade.text = filteredMed[indexPath.row].CIDADE
    } else {
        let mNome = myCell.viewWithTag(11) as! UILabel
        let mEspecialidade = myCell.viewWithTag(12) as! UILabel
        let mCorpoClinico = myCell.viewWithTag(13) as! UILabel
        let mCidade = myCell.viewWithTag(14) as! UILabel

        mNome.text = tableViewDataSource[indexPath.row].NOME

        mEspecialidade.text = tableViewDataSource[indexPath.row].ESPECIALIDADE
        mCorpoClinico.text = tableViewDataSource[indexPath.row].CORPOCLINICO

        mCidade.text = tableViewDataSource[indexPath.row].CIDADE

        mNome.frame = CGRect(x: mNome.frame.origin.x, y: mNome.frame.origin.y, width: mNome.frame.width, height: mNome.optimalHeight)

        mCorpoClinico.frame = CGRect(x: mCorpoClinico.frame.origin.x, y: mCorpoClinico.frame.origin.y, width: mCorpoClinico.frame.width, height: mCorpoClinico.optimalHeight)

    }

    return myCell
}

[SOLVED] I was able to solve I took the items of json and assigned some variable. Let arrayCC = tableViewDataSource[indexPath.Row]. CORPOCLINIC

then I did a split with joined
Let arrayCorpo = arrayCC.split(separator: "|"). joined(separator: " n n")

  • Missing the code you have for us to check

  • [SOLVED] I was able to solve I took the items of json and assigned a variable any Let arrayCC = tableViewDataSource[indexPath.Row]. CORPOCLINICO dai did a split with joined Let arrayCorpo = arrayCC.split(separator: "|"). joined(separator: " n n")

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.