您好,我已经尝试了几种方法,但基本上,我的 firebase 数据库中有一组数据,是我使用 Python 上传的。在 Python 中,我将所有内容添加到一个数组并将其上传到数据库。在 Swift 中,我尝试像这样检索数据
import UIKit
import Foundation
import Firebase
class CSGOView: UIViewController, UITableViewDelegate, UITableViewDataSource{
var teams: [String] = []
var times: [String] = []
@IBOutlet weak var tableViewTwo: UITableView!
let ref = FIRDatabase.database().reference(fromURL: "Took the url out it is the right url to the database im sure")
override func viewDidLoad() {
super.viewDidLoad()
getTeamsAndTimes()
}
func getTeamsAndTimes() {
//let userID = FIRAuth.auth()?.currentUser?.uid
ref.child("Teams").observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
let value = snapshot.value as? [String: String] ?? [:]
for team in value {
print(team.key)
print(team.value)
}
// ...
}) { (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!
}
}
虽然当 View 加载时没有任何内容打印到控制台,但这不起作用。我在 ViewDidLoad() 中运行该方法。
这是数据在 firebase 数据库中的样子(我必须去 所以我回来时会上传它)大致就像
团队 0:“团队名称” 1:“团队”
所以有团队,然后是一个加号,它会下拉以显示一个以 0 为键、“teamname”为值的字典。如果有帮助的话,我在键和值之前没有随机 ID。我在 python 中使用了 set 方法而不是 push 方法。 set方法上传没有id的数据。
事实证明,在我有 10 个代表之前我无法上传图片。
上面的链接带您了解 firebase 数据库的外观
最佳答案
在 JavaScript 中,您可以直接提供对 firebaseArray 的引用,并从 firebase 中获取数组形式的值。
var TeamRef = ref.child('Teams');
var TeamInfo = $firebaseArray(TeamRef);
不确定 Swift/Python。
关于python - 如何从 firebase 检索数据数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42869431/