This code is used to answer the question here:
How to make an HTTP request in Swift?
let url = NSURL(string: "http://www.stackoverflow.com")
let task = NSURLSession.sharedSession().dataTaskWithURL(url) {(data, response, error) in
println(NSString(data: data, encoding: NSUTF8StringEncoding))
}
task.resume()
The docs for dataTaskWithURL
say the following:
func dataTaskWithURL(_ url: NSURL,
completionHandler completionHandler: ((NSData!,
NSURLResponse!,
NSError!) -> Void)?) -> NSURLSessionDataTask
So it appears the {(data .... }
portion of the first code block is the completion handler. I come from a Java background where this would be expressed like so:
dataTaskWithUrl(url, function(data, ....) { .... });
Could someone explain why the completion handler is not the second argument in the method call?