1

I'm trying to handle a completed URL request in Swift 2.2 and check for errors but the line starting with completionHandler:{(response: NSURLResponse... in the code below is throwing the error:

cannot convert value of type '(NSURLREsponse!, NSData!, NSError!) - Void' to expected argument type '(NSURLResponse?, NSData?, NSError?) -> Void'.

I have a suspicion I need to use a do-try-catch but I'm not certain if there's a simpler way or not.

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(),
    completionHandler: {
        (response: NSURLResponse!, data: NSData!, error: NSError!) - > Void in
        if error == nil {
            var image = UIImage(data: data)

            dispatch_async(dispatch_get_main_queue(), {
                cell.selfieImgView.image = image
            })
        } else {
            print("Error: \(error.localizedDescription)")
        }
    })

return cell
}
Alexandre Neukirchen
  • 2,713
  • 7
  • 26
  • 36
Nick
  • 71
  • 2
  • 11

1 Answers1

6

Your closure signature must match the signature expected by

NSURLConnection.sendAsynchronousRequest()

Define it like the error message suggest:

completionHandler: {(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in ...
  • Worked great. Thanks Rasmus you rock! I'd be upvoting but I don't have a level 15 reputation yet – Nick Apr 21 '16 at 19:59
  • You wouldn't happen to know what the reason they've changed from the bang ! in Swift 1 to the ? in Swift 2 is would you? – Nick Apr 21 '16 at 20:07
  • No, but maybe you should take a look at this: http://stackoverflow.com/questions/30935304/sendasynchronousrequest-was-deprecated-in-ios-9-how-to-alter-code-to-fix – Rasmus Friis Kjeldsen Apr 21 '16 at 20:13