我正在从Firebase Firestore(不是auth)检索数据,并希望将其存储在字典中。当它遍历文档时,应拉出每个键及其各自的值,并将其存储在字典中。但是,它不会这样做。代码如下:
extension HomeViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = JobListingTable.dequeueReusableCell(withIdentifier: "JobListing", for: indexPath as IndexPath)
var data = fetchPostData()
print(data)
return cell
}
func fetchPostData() -> [String: Any] {
let db = Firestore.firestore()
var dataArray = ["1": "1" as Any]
db.collection("posts").getDocuments { (query, err) in
if err != nil{
print("error retrieving data")
} else {
print("worked")
for documents in (query?.documents)!{
let dataUser = documents.dictionaryWithValues(forKeys: ["HirerUID"])
for dictObjects in dataUser {
dataArray[dictObjects.key] = dictObjects.value
}
}
}
}
return dataArray
}
}
它只会反复打印出带有伪值的字典,直到停止循环为止。