我有这样的代码:
ref.child("skelbimai").observeSingleEvent(of: .value, with: {(snapshot) in
for child in (snapshot.children.allObjects as? [DataSnapshot])!{
let pinas = PinColorAnotation(color: UIColor.red)
pinas.coordinate = CLLocationCoordinate2D(latitude: child.childSnapshot(forPath: "lat").value as! Double, longitude: child.childSnapshot(forPath: "lon").value as! Double)
pinas.pinColor = Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Color
pinas.title = String(Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Name)
pinas.subtitle = String(child.childSnapshot(forPath: "price").value as! Double)
self.Map.addAnnotation(pinas)
}
})
问题是我无法将我的引脚保存到阵列。我有数组Pins = PinColorAnotation,当我调用Pins.append(pinas)并打印Pins.count时我总是得到0.为什么?但是当我在内部循环调用print时显示40.问题是我必须在从firebase下载之后对引脚进行排序和管理。但我不能。如何解决这个问题?我是否需要在以下方面执行所有管理逻辑:
ref.child("skelbimai").observeSingleEvent(of: .value, with: {(snapshot) in
for child in (snapshot.children.allObjects as? [DataSnapshot])!{
let pinas = PinColorAnotation(color: UIColor.red)
pinas.coordinate = CLLocationCoordinate2D(latitude: child.childSnapshot(forPath: "lat").value as! Double, longitude: child.childSnapshot(forPath: "lon").value as! Double)
pinas.pinColor = Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Color
pinas.title = String(Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Name)
pinas.subtitle = String(child.childSnapshot(forPath: "price").value as! Double)
self.Map.addAnnotation(pinas)
}
})
答案 0 :(得分:0)
这样的事情应该有效:
func getPins(completion: @escaping ([PinColorAnnotation]) -> Void){
var data: [PinColorAnnotation] = []
ref.child("skelbimai").observeSingleEvent(of: .value, with: {(snapshot) in
for child in (snapshot.children.allObjects as? [DataSnapshot])!{
let pinas = PinColorAnotation(color: UIColor.red)
pinas.coordinate = CLLocationCoordinate2D(latitude: child.childSnapshot(forPath: "lat").value as! Double, longitude: child.childSnapshot(forPath: "lon").value as! Double)
pinas.pinColor = Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Color
pinas.title = String(Kategorija(child.childSnapshot(forPath: "cat").value as! Int).Name)
pinas.subtitle = String(child.childSnapshot(forPath: "price").value as! Double)
self.data.append(pinas)
}
completion(data)
})
}
然后,您可以在任何需要的地方调用该功能,例如viewDidLoad
。然后可以将返回的数据设置为self.map
数组。
如果有任何错误我会道歉,它已经在平板电脑上完成,因为我不在电脑附近。