-1

I just updated my Xcode and have been trying to learn more about it. This is my code for Collection View Cell. I'm trying to get data from URL but I've been annoyed with this error. I tried all the solutions in here but they have different function structure. Also other solutions didn't work.

import UIKit


class PersonCell: UICollectionViewCell {

@IBOutlet weak var personImage : UIImageView!



    func ConfigureCell (imgURL : String)
{

}

    func DownloadImage ( url : NSURL)
{

}

    func GetDatafromURL (URL : NSURL , completion :   @escaping ((   _ data :     NSData? , _ response : URLResponse? ,  _ error : NSError?) -> Void))
{
        URLSession.shared.dataTask(with: URL) { (data ,  response , error) in     
            completion (data , response, error)     
            }.resume()   
}
}

the code that worked in the tutorial video is something like this

    func GetDatafromURL (URL : NSURL , completion : ((  data : NSData? , response : NSURLResponse? ,  error : NSError?) -> Void))
{


    NSURLSession.sharedSession.dataTaskWithURL( URL) { (data ,  response , error) in

        completion(data : data , responce : response, error : error)


        }.resume()
AlmoDev
  • 969
  • 2
  • 18
  • 46

2 Answers2

2

You can convert your NSURL to a URL using .absoluteURL

guard let url = URL.absoluteURL else { return }

URLSession.shared.dataTask(with: url) { (data, response, error) in
    //
}.resume()

UPDATE

Your completion block has the wrong types. You're using NSData instead of Data and NSError instead of Error. Here's an example of what it should look like below.

func getData(from url: URL, completion: @escaping (_ data: Data?, _ response: URLResponse?, _ error: Error?) -> Void)
{
    URLSession.shared.dataTask(with: url) { data, response, error
        completion(data, response, error)
    }.resume()
}
Callam
  • 11,409
  • 2
  • 34
  • 32
  • Both of your solutions give me the same error that I cannot invoke " dataTask" with an argument of type '(with : URL,Data?,URLResponse>,Error?) – AlmoDev Sep 26 '16 at 13:21
2

Please check the latest reference.

func dataTask(with: URL, completionHandler: (Data?, URLResponse?, Error?) -> Void)

Declaration

func dataTask(with url: URL,
     completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) -> URLSessionDataTask

In Swift 3, the completionHandler of dataTask(with:completionHandler:) takes 3 arguments with types Data?, URLResponse? and Error?. So, your method's completion, which is of type (NSData?,URLResponse?,NSError?) -> Void cannot be applied.

And it takes URL as its first parameter.

(Thanks to vadian, he suggested all needed things here.)

So, your GetDatafromURL needs to be something like this:

func GetDatafromURL(url: URL, completion: @escaping ((_ data: Data?, _ response: URLResponse?,  _ error: Error?) -> Void))
{
    URLSession.shared.dataTask(with: url) { (data, response, error) in
        completion (data , response, error)
    }.resume()
}
OOPer
  • 47,149
  • 6
  • 107
  • 142