I am trying to download some images and found a solution on Stack Overflow
It is using some closure syntax I don't understand.
func getDataFromUrl(url: URL, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
URLSession.shared.dataTask(with: url) { data, response, error in
completion(data, response, error)
}.resume()
}
It creates a function, and it is allowed to escape the completion argument, right? Ths function then returns a URLSession? where is the return statement?
What does the "data, response, error in" statement mean?
Then it calls a function called completion(...) where is it defined and implemented?
I guess this is some syntax I don't know. Any hint on where to read about this?
I then called the function:
getDataFromUrl(url: url){ data, response, error in
guard let data = data, error == nil else {return}
DispatchQueue.main.async {
let image = UIImage(data: data)
print(image!)
self.m_images?.append(image!)
print(self.m_images!.count)
}
}
self.m_images is an Array: [Any], declared like this at the top of my viewController:
var m_images:[Any]?=nil
I am getting an error when unwrapping it, it is still nil: Why? I just appended an image. The image does exist, I print it and it works fine.