如何从Firebase检索数据数组到iOS App

时间:2018-06-08 11:16:02

标签: swift firebase-realtime-database

我正在尝试从Firebase DB中检索此内容:

Firebase DB

这是我的VC,我从DB中检索数据:

https://us-central1-<your-project-id>.cloudfunctions.net/sendMail

如果我尝试为每一行加载一个值,它可以正常工作,但我无法获取它来检索数组。

这就是我创建课程的地方&#34; Datas&#34;:

//
//  VestibularesViewController_Design.swift
//  newProject
//
//  Created by Lucas Nascimento on 31/05/18.
//  Copyright © 2018 Lucas Frazao. All rights reserved.
//

import UIKit
import Firebase

class VestibularesViewController_Design: UIViewController, UITableViewDataSource, UITableViewDelegate {

    @IBOutlet weak var topView: UIView!

    @IBOutlet weak var tableView: UITableView!

    @IBOutlet var backgroundView: UIView!

    @IBOutlet weak var nomeVestibular: UILabel!

    var ref: DatabaseReference!
    var databaseHandle: DatabaseHandle?


    var datas = [Datas]()

    var newItems: [Datas] = []

    var meses = ["Maio","Junho","Agosto", "Agosto"]

    var ano = ["2018", ""]

    var dias = ["18","20", "30", "31"]

    var eventos = ["Inicio das inscrições", "Fim das inscricoes", "1ª Prova", "2ª Prova"]


    override func viewDidLoad() {
        super.viewDidLoad()

        nomeVestibular.text = "ENEM"

        self.navigationController?.navigationBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
        self.navigationController?.navigationBar.isTranslucent = true

        loadPosts()
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return datas.count
    }

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


    func loadPosts() {

        Database.database().reference().child("Datas").observe(.childAdded, with: { (snapshot) in

            DispatchQueue.main.async {
                self.tableView.reloadData();
            }

            if let dict = snapshot.value as? [String: [Any]] {

                let diaText = dict["dia"] as? String
                let mesText = dict["mes"] as? String
                let eventoText = dict["evento"] as? String
               // let dateText = dict["date"] as? String
                let data = Datas(diaText: diaText, mesText: mesText, eventoText: eventoText)
                self.newItems.append(data)
                print(self.newItems)

            }
        })
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {


        let cell = tableView.dequeueReusableCell(withIdentifier:"cell", for: indexPath) as! VestibularesTableViewCell

        //cell.titleNews.text = "O Edital do ENEM foi anunciado!"

        ref = Database.database().reference()

        cell.dia.text = datas[0].dia

        cell.evento.text = datas[indexPath.row].evento

        cell.mes.text = datas[indexPath.row].mes

        if backgroundView.backgroundColor == UIColor.white {

            cell.mes?.textColor = UIColor.black

            cell.dia?.textColor = UIColor.black

            cell.evento?.textColor = UIColor.black
        }
        return cell
    }



    override func viewWillDisappear(_ animated: Bool) {

        //self.navigationController?.setNavigationBarHidden(false, animated: true)
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

我认为需要更改与Datas类相关的内容才能使其正常工作。

1 个答案:

答案 0 :(得分:2)

您正在尝试将数组读入字符串:

import docx
document = docx.Document()
run = document.add_paragraph().add_run()
'''Apply style'''
style = document.styles['Normal']
font = style.font
font.name = 'MS Gothic'
font.size = docx.shared.Pt(15)
paragraph = document.add_paragraph('Some text\n')
'''Add another sentence to the paragraph'''
sentence = paragraph.add_run('A new line that should have a different font')
'''Then format the sentence'''
sentence.font.name = 'Arial'
sentence.font.size = docx.shared.Pt(10) 

那不行。我不是Swift专家,但最有可能的是:

let eventoText = dict["evento"] as? String