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!
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With