Reputation: 1301
I'm trying to use Alamofire for a data request. Previously my code for this task without Alamofire was:
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: { (data, response, error) in
//download hit an error so lets return out
if error != nil {
print(error)
return
}
DispatchQueue.main.async(execute: {
if let downloadedImage = UIImage(data: data!) {
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}
})
}).resume()
And trying to use Alamofire, I tried:
let url = URL(string: urlString)
Alamofire.request(.get, url)
.responseImage { response in
DispatchQueue.main.async(execute: {
if let downloadedImage = response.result.value {
// image is here.
imageCache.setObject(downloadedImage, forKey: urlString as NSString)
self.image = downloadedImage
}else{
// check what happened.
}
})
}
But I'm getting an error on url
in the request parameters, "extra argument in call". I checked the other questions on the same issue here on SO and they all seemed to be trying to pass in different parameters than just a URL so I'm not sure how I can apply those answers.
Thanks for any help.
Upvotes: 1
Views: 178
Reputation: 437582
The Alamofire.request
parameters have changed. The first parameter used to be the HTTP method, but now the URL is the first parameter and the method
argument is second (and is optional):
let url = URL(string: urlString)! // note, unwrap the optional URL
Alamofire.request(url, method: .get)
.responseImage { ... }
Or simply:
let url = URL(string: urlString)!
Alamofire.request(url)
.responseImage { ... }
Or, bypass URL
altogether:
Alamofire.request(urlString)
.responseImage { ... }
Unrelated, the DispatchQueue.main.async
is not needed with Alamofire. Unlike URLSession
, Alamofire already runs its completion handlers on the main queue (unless you supply a queue
parameter to request
method). So, eliminate the DispatchQueue.main.async
call.
Upvotes: 1