【问题标题】:Extracting only the Double from a firebase snapshot (swift)从 firebase 快照中仅提取 Double (swift)
【发布时间】:2021-05-26 16:11:11
【问题描述】:

这是我在 firebase 中的数据:

覆盖 func viewDidLoad() {

    database.child("allOrdersTimeline1").observeSingleEvent(of: .value, with: { (snapshot) in
        var mixedArray1 = [:] as [String : Double]
        let salad = (snapshot.value)
        let keyy = snapshot.key
        let valuee = (snapshot.children.allObjects) //as [String : Double])
        
        
        
        

        self.labelWhatLabel.text = ("\(valuee)")
        print("There are \(snapshot.childrenCount) children found")
        print("1")
        print(keyy)
        print("2")
        print(valuee)
        print("3")
        print(salad)
        print("4")
       
        print("Ny test: \(mixedArray1)")
    
        
        
    })

无论我尝试什么,我都会得到完整的线路(姓名和时间),如下所示: 我不知道如何在没有名字的情况下只获得小时/双倍。

(顺便说一句,我很新,这可能很愚蠢 - 我真诚的道歉。

【问题讨论】:

  • 请尝试删除与问题无关的代码
  • 我尽可能地清理它。剩下的代码是我试图将firebase数据作为两个元素获取。名称为字符串,小时为 Double - 所以我可以在列表中使用小时。希望它澄清一点。
  • 如果你这样做let salad = snapshot.value as? [String: Any]会发生什么?
  • 相同的结果,我得到了字符串和双精度 - 作为一个。
  • 这与问题完全无关,但您可能想要更改存储节点密钥的方式。节点键必须是唯一的,那么当您拥有 John Smith 和 Larry Smith 时会发生什么?此外,节点键不能包含某些字符 - 如果您查看输出,您已经可以看到 S/U00F8rensen 的作用。所以更好的方法是使用.childByAutoId创建节点键,然后将姓氏和数量存储为子节点;它看起来像这样some_node/name: Agerskov 然后/price: 19

标签: swift firebase firebase-realtime-database


【解决方案1】:

我想你可能正在寻找:

print(snapshot.childSnapshot(forPath:"Agerskov").value)

如果不知道子节点的key,可以loop over snapshot.children访问子快照:

for child in snapshot.children {
  let snap = child as! DataSnapshot
  print(snap.key)
  print(snap.value)
}

当您搜索 [firebase-realtime-database][swift] loop over children 时也会看到这些结果。

【讨论】:

  • 我不知道我可以在这里使用循环!做到了!那是我的大问题,因为我不知道钥匙!它解决了问题和奖励,我现在知道我可以在“快照”中循环!谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-05-27
  • 1970-01-01
  • 2016-06-05
  • 1970-01-01
  • 2018-01-21
  • 2017-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多