Reputation: 23
func downloadCurrentWeather(completed: @escaping DownloadComplete){
Alamofire.request(API_URL).responseJSON { (response) in
let result = response.result
let json = JSON(result.value) // <-- (EXPRESSION IMPLICITLY COERCED WARNING)
self._cityName = json["name"].stringValue
let tempDate = json["dt"].double
let convertedDate = Date(timeIntervalSince1970: tempDate!)
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
dateFormatter.timeStyle = .none
let currentDate = dateFormatter.string(from: convertedDate)
self._date = "\(currentDate)"
self._weatherType = json["weather"][0]["main"].stringValue
let downloadedTemp = json["main"]["temp"].double
self._currentTemp = (downloadedTemp! - 273.15).rounded(toPlaces: 0)
completed()
}
}
Upvotes: 1
Views: 2072
Reputation: 438212
It's coercing it because value
is a Any?
optional value. I'd suggest unwrapping the value
to make sure it's not nil
:
func downloadCurrentWeather(completed: @escaping DownloadComplete){
Alamofire.request(API_URL).responseJSON { (response) in
guard let value = response.result.value else {
print(response.result.error ?? "Unknown error")
return
}
let json = JSON(value)
...
}
}
As a further refinement, I'd change DownloadComplete
to include information about whether it failed or not. For example, I might add an Error?
parameter, and then you could do:
func downloadCurrentWeather(completed: @escaping DownloadComplete){
Alamofire.request(API_URL).responseJSON { (response) in
guard let value = response.result.value else {
completed(response.result.error)
return
}
let json = JSON(value)
...
completed(nil)
}
}
Then the caller could see if the error
was nil
or not.
The other approach is to switch
on response.result
, because in the .success
case, you can just use the associated value:
func downloadCurrentWeather(completed: @escaping DownloadComplete){
Alamofire.request(API_URL).responseJSON { response in
switch response.result {
case .failure(let error):
completed(error)
case .success(let value):
let json = JSON(value)
...
completed(nil)
}
}
}
Upvotes: 3