gpt4 book ai didi

ios - 如何将 Firebase 快照中的变量分配给子节点中的值?

转载 作者:行者123 更新时间:2023-11-30 12:32:04 27 4
gpt4 key购买 nike

我正在尝试尽量减少向 Firebase 发出的数据请求量。我有一个提取以下数据的快照:

snap (Hole1) {
GreenBackX = "40.196339";
GreenBackY = "-105.07354";
GreenCenterX = "40.196336";
GreenCenterY = "-105.073409";
GreenFrontX = "40.196342";
GreenFrontY = "-105.073283";
Hazard1 = {
CarryX = "40.196839";
CarryY = "-105.07104";
FrontX = "40.196893";
FrontY = "-105.070626";
Name = "R Fwy Bunker";
};
Hazard2 = {
CarryX = "40.196321";
CarryY = "-105.071922";
FrontX = "40.196383";
FrontY = "-105.071573";
Name = "L Fwy Bunker";
};
Hazard3 = {
CarryX = "40.196622";
CarryY = "-105.072935";
FrontX = "40.196662";
FrontY = "-105.072554";
Name = "R Fwy Bunker 2";
};
Hazard4 = {
CarryX = "40.196176";
CarryY = "-105.073545";
FrontX = "40.196225";
FrontY = "-105.073167";
Name = "L Greenside Bunker";
};
HoleH = "258.74";
HoleX = "40.19664";
HoleY = "-105.070484";
}

我的目标是分配一个变量,即Hazard1\Name。到目前为止,我只在文档中看到了工作示例,展示了如何获取初始值,我喜欢这样: letholeX = value?["HoleX"] as?双倍的 ?? 0,但如果你愿意的话,我似乎找不到任何能让我达到“第二级”的东西。

有一个示例似乎表明您可以通过以下方式引用它来做到这一点:let Hazard1Name = value?["Hazard1/Name"] 但我无法让它工作。

最佳答案

FIRDataSnapshot 包含来自给定 Firebase 数据库位置的数据。数据已经下载完毕,因此没有太多优化空间,抱歉:-(

我认为您可能正在寻找的是:

let ref: FIRDatabaseReference = ...
ref.child("Hazard1").child("Name").observeSingleEvent(of: .value) {
(snapshot) in
let name = snapshot.value as! String
print("Name: \(name)")
}

否则,如果您只是想要从上述快照中获取数据,请尝试以下操作:

let snapshot: FIRDataSnapshot = ...
let data = snapshot.value as! [String: Any]
let hazard1 = data["Hazard1"] as! [String: Any]
let name = hazard1["Name"] as! String

关于ios - 如何将 Firebase 快照中的变量分配给子节点中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43403268/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com