Removing the Dictionary object on condition - Firebase - Swift

Asked

Viewed 60 times

1

In my App I need to list information registered by the user, there is an "enable" field where "on" will not be displayed in the tableViewController, if "yes" will list, code below for help. First item should not be displayed. Thank you.

Imagem da tableViewController

    let snapshot = self.listaDadosCombustivel[indexPath.row]
    let key = snapshot.key 

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "celulaDados", for: indexPath) as! CadastroDadosCell



    let snapshot = self.listaDadosCombustivel[indexPath.row]
    let key = snapshot.key
    let snapshotAnterior = self.listaDadosCombustivel[indexPath.row.littleEndian]
    self.idCadCombustivelAnterior = snapshotAnterior.key

    if var dados = snapshot.value as? [String : Any]{
        if let enable = dados["enable"] as? String{
            if enable == "yes"{
                if let dataAbastecimento = dados["dataAbastecimento"] as? String{
                    if let valorTotal = dados["valorTotal"] as? String{
                        if let litrosTotal = dados["litroTotal"] as? String{
                            if let kmAtual = dados["kmAtual"] as? String{
                                if let combustivel = dados["combustivel"] as? String{
                                    if let consumo = dados["consumo"] as? String{
                                        cell.dataLabel.text = dataAbastecimento
                                        cell.valorTotalLabel.text = valorTotal
                                        cell.litrosTotalLabel.text = litrosTotal
                                        cell.combustivelLabel.text = combustivel
                                        cell.kmVeiculoLabel.text = kmAtual
                                        cell.kmLitroLabel.text = consumo
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                print(key)
            }
        }            
    }
    return cell
}
  • it would be interesting you research on Codable, to make a Parser of this structure (listDadosCombustivel).

1 answer

2

Try this.

struct Combustivel: Codable {

    var enable: String?
    var dataAbastecimento: String?
    var valorTotal: String?
    var litroTotal: String?
    var kmAtual: String?
    var combustivel: String?
    var consumo: String?

}

class CadastroDadosCell: UITableViewCell {

    @IBOutlet weak var dataLabel: UILabel?
    @IBOutlet weak var valorTotalLabel: UILabel?
    @IBOutlet weak var litrosTotalLabel: UILabel?
    @IBOutlet weak var combustivelLabel: UILabel?
    @IBOutlet weak var kmVeiculoLabel: UILabel?
    @IBOutlet weak var kmLitroLabel: UILabel?
}

class ViewController: UIViewController {

 @IBOutlet weak var tableView: UITableView!

    var combList: [Combustivel] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        // ...
       guard let data = data else { return }

            do {
                let combustivels = try JSONDecoder().decode([Combustivel].self, from: data)

                if combustivels != nil{
                   self.combList = combustivels
               }else {
                   print("nothing to display")
               }

               DispatchQueue.main.async {
                  self.tableView.reloadData()
               }
             } catch let jsonErr {
            print("Error serializing json:", jsonErr)
        }
    }
//...
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return combList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "celulaDados", for: indexPath) as! CadastroDadosCell

    let item = self.dataSource[indexPath.row]

    if item.enable == "yes" {
        cell.dataLabel?.text = item.dataAbastecimento
        cell.valorTotalLabel?.text = item.valorTotal
        cell.litrosTotalLabel?.text = item.litroTotal
        cell.combustivelLabel?.text = item.combustivel
        cell.kmVeiculoLabel?.text = item.kmAtual
        cell.kmLitroLabel?.text = item.consumo
    }
    return cell
}
}
  • if combustivels != nil{ ? Decode would Return a non optional Object, in case it fails it would just throw it doesn’t Return nil. If you would like to Return an optional you would need to remove the do catch and change it to try? to ignore the error and Return nil in case of Failure

Browser other questions tagged

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