好的,所以这是我的代码:
var locationCount: Int!
var latLocation = [Double]()
var longLocation = [Double]()
func polyline() {
var coords = [CLLocationCoordinate2D]()
databaseHandle = databaseRef.child("RunList").child(runName).child("locations").observe(.value, with: { (snapshot) in
self.locationCount = Int(snapshot.childrenCount)
print(self.locationCount)
func getLocations() {
var i = 0
while i < self.locationCount {
self.databaseHandle = self.databaseRef.child("RunList").child(self.runName).child("locations").child("\(i)").observe(.value, with: { (snapshot) in
let locData = snapshot.value as? [String: AnyObject]
let lat = (locData?["lat"] as? Double)!
let long = (locData?["long"] as? Double)!
self.latLocation.append(lat)
self.longLocation.append(long)
})
i = i + 1
}
}
getLocations()
})
}
所以我面临的问题是,当我尝试在代码块/折线()之外调用两个数组latLocation和longLocation时,它们返回为空。因此,例如,如果我尝试在viewDidLoad()中打印它们,它们都打印为空数组。我怎样才能解决这个问题?同样的问题也在于locationCount,如果我在代码块之外打印,那么我得到一个错误,其中值为nil,因为它没有值,但在代码块内部它完全正常。这真让我感到困惑,我认为有一个简单的解决方案,我忽略了它。