Problem calling dataTask on URLSession

I'm pretty new to Swift. Have many years of experience in Objective-C. I'm trying to convert an Objective-C class to Swift as an exercise. I'm having a problem calling dataTask function in URLSession:

let task = session.dataTask(with: url) { (taskData, taskResponse, error) in

The compiler keeps giving me a warning that there is an "Invalid conversion from throwing function of type '(_, _, _) throws -> ()' to non-throwing function type '(Data?, URLResponse?, Error?) -> Void'". Looking in the documentation for the dataTask func, I don't see any mention of the method throwing an exception.

What am I missing?

Hello Laurent,

I think you are using try in your request completion handler, but do not handle eventual errors.

Wrap your try in do { ... } catch { ... }, handle the error, and the compiler will be assured that no error can escape!

Thanks, that did it!

1 Like
Terms of Service

Privacy Policy

Cookie Policy