Hi! I encountered a way of specifying completion handler that I don't understand. I am trying to find some swift syntax. Can you help me to find a link or some hint to understand how this works?
Following the example in https://www.raywenderlich.com/3244963-urlsession-tutorial-getting-started
I got the following code working and printing the expected message. However, the way it specify completion handler is not to pass the closure into the "dataTask" method call as a parameter, but just placing after the method call.
var sharedSession : URLSession = URLSession.shared
var googUrl:URL = URL(string: "https://www.google.ca/finance")!
var task1: URLSessionDataTask = sharedSession.dataTask(with: googUrl) {
data, response, error in
print("task 1 completed.")
}
I looked at the reference at https://developer.apple.com/documentation/foundation/urlsession
Seems the right way to specify the handler is passing it as parameter as follow
var task1: URLSessionDataTask = sharedSession.dataTask(with: googUrl, completionHandler: {
data, response, error in
print("task 1 completed.")
})
Why the former works?
Thank you!