I have this function, and I want to fill the locations array with latitude/longitude data that I get in the dataTask function.
The problem is that the data is only available in the datatask function and outside it, it'll be gone.
func getFromDatabase()
{
var locations: [CLLocationCoordinate2D] = []
let url = URL(string: URL_DATABASESend)
let task = URLSession.shared.dataTask(with: url!) { (data, response, error) in
//Here I print the JSON: print(NSString(data: data!, encoding: String.Encoding.utf8.rawValue)!)
do{
if(data != nil){
let parsedData = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [[String:String]]
for dic in parsedData! {
if let lat = Double(dic["latitude"]!), let long = Double(dic["longitude"]!) {
//print(lat)
//print(long)
var coordinatesToAppend = CLLocationCoordinate2D(latitude: lat, longitude: long)
locations.append(coordinatesToAppend)
}
}
}} catch let error as NSError{
print(error.localizedDescription)
return
}
}
task.resume()
}
I don't know how to use a completionhandler and when I do I get a lot of errors (I followed a lot of tutorials/online help topics).
Does anyone know how to I can get the information out of the function?(can't return the value cause I'm using datatask) I read a lot of topics but none of it solved my problem.
Hope to hear something from you guys!