如何从firebase中删除数组中项目之前的“snap”

时间:2017-03-18 06:41:21

标签: arrays swift database firebase

我有一个从firebase数据库返回数组的函数。问题是它总是在返回数据库中的值之前打印snap(项目所在的数字,例如ex.0),例如它将打印[snap(0)teamname]

如何在值的开头删除此快照(num)。也许有某种框架可以从数组中删除某些短语?让我知道谢谢,这是我的代码和数据库。

import UIKit
import Foundation
import Firebase


class CSGOView: UIViewController, UITableViewDelegate, UITableViewDataSource{

    var teams: [String] = []
    var times: [String] = []




    @IBOutlet weak var tableViewTwo: UITableView!
    let teamsRef = FIRDatabase.database().reference(fromURL: "can't have this info sorry, basically it goes straight to Teams in the database")

    override func viewDidLoad() {
        super.viewDidLoad()

        getTeamsAndTimes()
    }

    func getTeamsAndTimes() {
        teamsRef.observeSingleEvent(of: .value, with: { (snapshot) in
            let d = snapshot.children.allObjects
            print(d)
            for child in snapshot.children {
                print(child)
            }
        }) { (error) in
            print(error.localizedDescription)
        }
    }


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


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

        let cell: UITableViewCell? = nil
        return cell!

    }

}

这是显示数据库的图像,以及它的布局。

https://ibb.co/dSp5ka

这是控制台输出

[Snap (0) FaZe Clan, Snap (1) Natus Vincere, Snap (2) Natus Vincere, Snap (3) FaZe Clan, Snap (4) HellRaisers, Snap (5) G2 Esports, Snap (6) G2 Esports, Snap (7) HellRaisers, Snap (8) NRG Esports, Snap (9) Counter Logic Gaming, Snap (10) Counter Logic Gaming, Snap (11) NRG Esports, Snap (12) OpTic Gaming, Snap (13) Rush, Snap (14) Rush, Snap (15) OpTic Gaming]
Snap (0) FaZe Clan
Snap (1) Natus Vincere
Snap (2) Natus Vincere
Snap (3) FaZe Clan
Snap (4) HellRaisers
Snap (5) G2 Esports
Snap (6) G2 Esports
Snap (7) HellRaisers
Snap (8) NRG Esports
Snap (9) Counter Logic Gaming
Snap (10) Counter Logic Gaming
Snap (11) NRG Esports
Snap (12) OpTic Gaming
Snap (13) Rush
Snap (14) Rush
Snap (15) OpTic Gaming

1 个答案:

答案 0 :(得分:0)

您需要访问snapshot.value才能获得您的团队名称。

func getTeamsAndTimes() {
    teamsRef.observeSingleEvent(of: .value, with: { (snapshot) in
        self.teams = []
        for child in snapshot.children {
            if let team = (child as! FIRDataSnapshot).value as? String {
                 self.teams.append(team)
            }               
        }
        //Reload the tabelView after that
        self.tableViewTwo.reloadData()
    }) { (error) in
        print(error.localizedDescription)
    }
}

现在在numberOfRowsInSection返回teams数组count而不是3

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


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

    let cell = tableView.dequeueReusableCell(withIdentifier: "YourIdentifer", for: indexPath)
    cell.textLabel?.text = teams[indexPath.row]
    return cell
}