Fabrizio Bartolomucci
Fabrizio Bartolomucci

Reputation: 4948

Cannot invoke 'dataTask' with an argument list of type

I have this vanilla piece of code I virtually use everywhere in my code:

let configuration = URLSessionConfiguration.default
let session = URLSession(configuration:configuration)
let listTask = session.dataTask(with: theRequest, completionHandler: {[weak self](data, response, error) in
})

Yet in one particular class the compiler complaints:

Cannot invoke 'dataTask' with an argument list of type '(with: URLRequest, completionHandler: (Data?, URLResponse?, Error?) -> ())'

Expected an argument list of type '(with: URLRequest, completionHandler: > (Data?, URLResponse?, Error?) -> Void)'

How does it infer the return value of the closure to be () instead of the expected Void? I copied the code from other classes repeatedly lest I wrote something wrongly.

Upvotes: 2

Views: 3010

Answers (2)

Fabrizio Bartolomucci
Fabrizio Bartolomucci

Reputation: 4948

For some reason adding:

return () 

at the end of the closure, as suggested on the Apple forum, fixed the issue.

Upvotes: 6

Ankit Thakur
Ankit Thakur

Reputation: 4749

tried in xcode 8 beta 6 with swift 3 playground

    let configuration = URLSessionConfiguration.default
    let session = URLSession(configuration:configuration)
    let theRequest = URLRequest(url: URL(string: "")!)
    let task = session.dataTask(with: URL(string: "")!) { (data:Data?, response:URLResponse?, error:Error?) in

    }

    let task1 = session.dataTask(with: theRequest) { (data:Data?, response:URLResponse?, error:Error?) in

    }

Upvotes: 2

Related Questions