17

Does anybody know how handlers (blocks) work in swift? I am trying to get this code running but i can't find any documentation of the right syntax for the completionHandler.

let url:NSURL = NSURL(string:"some url")
let request:NSURLRequest = NSURLRequest(URL:url)
let queue:NSOperationQueue = NSOperationQueue()

NSURLConnection.sendAsynchronousRequest(request:request, queue:queue, completionHandler handler:((NSURLResponse!, NSData!, NSError!) -> Void)!)
loopmasta
  • 1,693
  • 3
  • 14
  • 19

4 Answers4

41

Like this:

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ response, data, error in /* Your code */ })

Or more verbose variant.

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
    /* Your code */
})
Tomáš Linhart
  • 13,509
  • 5
  • 51
  • 54
  • 1
    Can anyone help me understand how to get data out of the NSURLConnection.sendAsynchronousRequest? The completionHandler requires a void return value, so I am not certain how to get my request data out of the completionHandler. thanks! – cosmikwolf Jul 16 '14 at 23:49
  • You have access for example to the other variables into the function where you call the class method sendAsynchronousRequest, e.g., var lol: String! = "a" NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in /* Your code */ lol = "b" }) – King-Wizard Feb 06 '15 at 05:02
3

You need to use this code:

NSURLConnection.sendAsynchronousRequest(request,queue:queue,completionHandler:{response,data,error in /* code goes here */ })

For more info, you can refer to this tutorial, or or check the answers to How to parse a JSON file in swift?.

Community
  • 1
  • 1
Bluewings
  • 3,438
  • 3
  • 18
  • 31
3

sendAsynchronousRequest has been deprecated in newer versions of Swift. Move to dataTaskWithRequest, luckily it is used pretty much the same way

let request:NSURLRequest = NSURLRequest(URL:NSURL(string:"http://YOUR_DESIRED_URL.com")!)
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let session = NSURLSession(configuration: config)

let task = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in

});

task.resume()
Chris Klingler
  • 5,258
  • 2
  • 37
  • 43
0

The right term you are looking for here is Closure. Closures in Swift are similar to blocks in C and Objective-C. In addition to Tomáš's answer there is another short version to use the completion handler here:

NSURLConnection.sendAsynchronousRequest(request, queue: queue, completionHandler: {$0; $1; $2})

Here I have used Shorthand Argument Names. I am accessing response as $0, data as $1 and error as $3. I find this syntax more easy to read and write unless the parameters are large in number otherwise the code will become unreadable.

Evol Gate
  • 2,247
  • 3
  • 19
  • 37