How to use NSURLConnection completionHandler with swift

前端 未结 4 2016
感动是毒
感动是毒 2020-12-25 14:33

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

4条回答
  •  鱼传尺愫
    2020-12-25 15:07

    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.

提交回复
热议问题