【问题标题】:how to add firebase element to dictionary如何将firebase元素添加到字典
【发布时间】: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


【解决方案1】:

谢谢你们,我通过这个伟大的扩展修复了它

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:

谢谢你们 祝你有美好的一天

【讨论】:

    猜你喜欢
    • 2023-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    • 2013-01-31
    • 1970-01-01
    相关资源
    最近更新 更多