Swift 3: Unable to call dataTask using argument list of type error

I'm starting to work on iOS. I searched the web and could not find an answer that would solve my problem. Stuck and has no idea what to do and how to look for a solution.

I follow the guide based on Swift 2. The following method shows an 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 made all the settings suggested by Xcode, but no additional tips.

In addition, the source code from the tutorial was as follows:

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

Xcode then suggested adding as URL , as shown below:

 let urlRequest = NSMutableURLRequest(url: URL as URL) 

In both versions it is displayed without errors . So what is the difference? Which one should I use?

I am very grateful for any help!

+5
source share
1 answer

In Swift 3, the compiler wants a native URLRequest

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

But with your specific syntax you don't even need a request

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

and annotations

 let task = session.dataTask(with: url) { (data, response, error) in ... 
+11
source

Source: https://habr.com/ru/post/1258106/


All Articles