gpt4 book ai didi

swift - Swift 3 中的 Firebase 数据库设置和数据处理

转载 作者:行者123 更新时间:2023-11-28 06:24:05 25 4
gpt4 key购买 nike

我对 swift 和 Firebase 还很陌生。我有这样的数据库:My database preview

在应用程序中,我有多个注释,我需要将信息从数据库传递给它们。在这一点上,我对将数据作为字典读取并将其传递给注释有点困惑。这是我之前不使用数据库而使用数组时的代码:

for i in 0...2
{
let coordinate = coordinates[i]
let point = myAnnotation(coordinate: CLLocationCoordinate2D(latitude: coordinate[0] , longitude: coordinate[1] ))
point.name = names[i]
point.address = addresses[i]
point.hours = hours[i]
point.phones = phones[i]

self.mapView.addAnnotation(point)
}

您不必为我编写代码,但我至少需要一些提示。

最佳答案

我的 firebase 上有一个类似的数据库,我会这样做:

FIRDatabase.database().reference().child("Data").observe(.value, with: {(snapshot) in
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
for snap in snapshot{
if let dict = snap.value as? [String: Any] {
if let address = dict["address"] as? String,let lat = dict["lat"] as? String, let long = dict["long"] as? String, let name = dict["name"] as? String {
let coordinate = coordinates[i]
let point = myAnnotation(coordinate: CLLocationCoordinate2D(latitude: lat , longitude: long ))
point.name = name
point.address = address

self.mapView.addAnnotation(point)
}
}
}
}
})

我为您的数据节点的每个子节点循环,将该快照值作为字典传递,然后读取字典并对其值进行类型转换以查看格式是否正确,然后将其分配并将其附加到数组。

关于swift - Swift 3 中的 Firebase 数据库设置和数据处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42509235/

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