Swift 3中的Firebase数据库设置和数据处理
我对swift和Firebase相当陌生。我有这样的数据库:Swift 3中的Firebase数据库设置和数据处理
在应用程序中我有多个注释,我需要将信息从数据库传递给他们。在这一点上,我对将数据作为字典读取并传递给注释感到困惑。 这是我以前的代码时,我没有使用数据库,使用数组:
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)
}
你不必为我的代码,但我至少需要一些提示吧。
我有我的火力点一个类似的数据库,我会做这样的:
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)
}
}
}
}
})
,我环路为单元值作为字典的数据节点,合格的每一个孩子,然后读字典和类型将其值转换为格式是否正确,然后将其赋值并将其附加到数组。
是的,这正是我想要做的,你救了我的一天。还有2个小错误1,Lat和Long必须是“as Double”,2,只需删除“let coordinate = coordinates [i]”这一行,非常感谢你的帮助。 –
很高兴帮助:D是啊我只是用字符串,这就是为什么我没有意识到你在哪里使用双打。 –
显示您正在尝试的Firebase基本代码。 –