Marat Marat - 1 year ago 415
iOS Question

Swift 3: Cannot invoke dataTask with an argument list of type error

I'm beginner in iOS. I have searched web and couldn't find answer that would solve my problem. Stuck and have no idea what to do and how to search for solution.

I'm following the tutorial which is based on Swift 2. The following method is showing error.

func downloadBooks(bookTitle: String) {
let stringURL = "GET\(bookTitle)"

guard let URL = URL(string: stringURL) else {
print("url problems")

let urlRequest = NSMutableURLRequest(url: URL)
let session = URLSession.shared

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



I have made all adjustments suggested by Xcode, but no further hints.

Moreover, original portion of code from tutorial was like this:

guard let URL = NSURL(string: stringURL) else {
print("url problems")

Then Xcode suggested to add
as URL
like below:

let urlRequest = NSMutableURLRequest(url: URL as URL)

Both of these versions are showing no error. So what is the difference? Which one should I use?

I would really appreciate any help!

Answer Source

In Swift 3 the compiler wants native URLRequest

let urlRequest = URLRequest(url: URL)

But with your particular syntax you don't even need the request

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

nor the annotations

let task = session.dataTask(with: URL) { (data, response, error) in ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download