我有空字典
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
}
答案 0 :(得分:0)
谢谢你们我通过这种出色的扩展解决了该问题
extension Dictionary where Value: RangeReplaceableCollection {
public mutating func append(element: Value.Iterator.Element, toValueOfKey key: Key) -> Value? {
var value: Value = self[key] ?? Value()
value.append(element)
self[key] = value
return value
}
}
self.orderArray.append(element: name2 as! String, toValueOfKey: "name")
但是我遇到了Firebase的新问题
喜欢
2019-04-03 00:56:48.969048+0200 Ml Matba5[1809:376762] *** Terminating app
due to uncaught exception 'InvalidPathValidation', reason:
'(child:) Must be a non-empty string and not contain '.' '#' '$' '[' or ']''
*** First throw call stack:
谢谢你们 祝你有美好的一天