【发布时间】:2019-08-23 20:11:35
【问题描述】:
我有这本空字典
let orderArray = ["name":"","quntity":"","price":""]
当我从 Firebase 下载数据时,我想获取一些元素并将它们放入字典中以便以后阅读
这是我的 Firebase 方法:
self.databaseRef.child("Users").child((Auth.auth().currentUser?.uid)!).child("cart").child(self.orderFromKitchen).observe(DataEventType.value, with: { (snapshot) in
//if the reference have some values
if snapshot.childrenCount > 0 {
//clearing the list
self.ordersList.removeAll()
//iterating through all the values
for info in snapshot.children.allObjects as! [DataSnapshot] {
//getting values
//let key = self.kitchensRef.childByAutoId().key
let infoObject = info.value as? [String: AnyObject]
let name = infoObject?["name"]
let Id = infoObject?["ID"]
let img = infoObject?["img"]
let price = infoObject?["price"]
let quantity = infoObject?["quntity"]
self.nameArr.append((name as! String?)!)
self.quaArr.append((quantity as! String?)!)
let info = ordersModel(id: Id as! String?, name: name as! String?, img: img as! String?, price: price as? Int,quantity:quantity as! String?)
//here i want to add name and price to the dictionary
self.orderArray.insert(contentsOf: "\(infoObject?["name"]!)", at: 0)
self.orderArray.insert(contentsOf: "\(infoObject?["price"]!)", at: 2)
self.ordersList.append(info)
}
//reloading the tableview
self.tableView.reloadData()
self.loadingView.isHidden = true
}
【问题讨论】:
-
var orderArray = ["name": "", "quntity": "", "price": ""]
-
if let name = infoObject?["name"], let price = infoObject?["price"], let quantity = infoObject?["quntity"] { orderArray["name"] = name orderArray["quntity"] = 数量 orderArray["price"] = price }
-
请记住,
["name":"","quntity":"","price":""]不是 数组,它是一个字典,如[String : String]。 -
@ElTomato 谢谢兄弟,它正在工作,但是“名称”中的所有元素,例如,当我打印它时,我得到
namename2name3和价格100200300这样我不知道是什么我必须做 -
@AhmadF 哦,好的,谢谢兄弟
标签: swift firebase dictionary firebase-realtime-database