0

I'm creating a medicine database as practice and I would like to load image of pills from URLs.

Here is part of my code and it says

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

How am I supposed to fix it ? Thank you!

func img_URL(urlString:String)
    {

        var imgURL: NSURL = NSURL(string: urlString)!
        let request: NSURLRequest = NSURLRequest(URL: imgURL)
        NSURLConnection.sendAsynchronousRequest(
            request, queue: NSOperationQueue.mainQueue(),
            completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in
                if error == nil {
                    self.pillsImage.image = UIImage(data: data)
                }
        })

    }
pikkuu
  • 71
  • 2
  • 11

2 Answers2

1

Do what the error message tells you! Change

completionHandler: {(response: NSURLResponse!,data: NSData!,error: NSError!) -> Void in

to

completionHandler: {(response: NSURLResponse?,data: NSData?,error: NSError?) -> Void in

Even better, abandon NSURLConnection. It is replaced by NSURLSession. The call you are making is deprecated and will be removed in a future system update (possibly as soon as this June).

matt
  • 515,959
  • 87
  • 875
  • 1,141
1
completionHandler: {(response: NSURLResponse?, data: NSData?, error: NSError?) -> Void in

will solve your issue

matt
  • 515,959
  • 87
  • 875
  • 1,141
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53