Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 https://www.googleapis.com/books/v1/volumes?q=\(bookTitle)"

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

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

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

    }

    task.resume()
}

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")
    return
}

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!

like image 409
Marat Avatar asked Oct 12 '16 21:10

Marat


1 Answers

In Swift 3 the compiler wants native URLRequest

let urlRequest = URLRequest(url: url) // use a lowercase variable name, URL is a native struct in Swift 3

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 ...
like image 120
vadian Avatar answered Nov 05 '22 13:11

vadian