【问题标题】:how to remove "snap" before the item in the array from firebase如何从firebase中删除数组中的项目之前的“snap”
【发布时间】:2017-03-18 06:41:20
【问题描述】:

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

我将如何摆脱值开头的这个 snap (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

【问题讨论】:

    标签: arrays swift database firebase


    【解决方案1】:

    您需要访问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
    }
    

    【讨论】:

    • 天哪。你是最好的家伙,整晚都在努力。感谢您甚至向我展示了如何更新单元格以及我非常感激的一切。如果我可能会问你是如何在 swift 中如此擅长编程的?您是否只是在一家 swift 应用程序制作公司工作您是否阅读并制作了很多应用程序?对我有什么建议吗?感谢您的回答,它完美地工作@Nirav D
    • @DevinTripp 欢迎伙伴 :) 伙伴,如果您阅读了苹果 swift 文档,那么您将获得足够的知识来使用 swift lang 开发应用程序,没什么大不了的 :)
    • 你告诉我你阅读了苹果的所有文档,这是你使用的唯一资源。我希望这很容易。我不懂很多语法,所以对我来说有点难。我接受了我非常感谢的答案D!
    • @DevinTripp 我并不是说要阅读整个文档,您可以阅读此编程指南 developer.apple.com/library/content/documentation/Swift/… 当您遇到某些特定类型的问题时,请阅读有关该类型的信息
    猜你喜欢
    • 1970-01-01
    • 2021-10-19
    • 2019-10-16
    • 1970-01-01
    • 2020-03-02
    • 2020-09-11
    • 2016-07-15
    • 2021-04-03
    • 1970-01-01
    相关资源
    最近更新 更多