Reputation: 2562
I am trying to create a weather App in Swift without using Podfiles and I have reached a stumbling block in terms of passing a dictionary as an argument in parsing JSON. The location function is as follows:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[locations.count - 1]
if location.horizontalAccuracy > 0 {
locationManager.startUpdatingLocation()
locationManager.delegate = nil
print("longitude = \(location.coordinate.longitude), latitude = \(location.coordinate.latitude)")
let latitude = String(location.coordinate.latitude)
let longitude = String(location.coordinate.longitude)
let params : [String : String] = ["lat" : latitude, "lon" : longitude, "appid" : APP_ID]
getWeatherData(url: WEATHER_URL, parameters: params)
}
}
So to parse the JSON I have created the following function:
private func getWeatherData(url: String, parameters: [String : String]) {
let JsonURLString:[String: Any] = ["url": WEATHER_URL, "parameters": parameters]
print(JsonURLString)
guard let url = URL(string: JsonURLString) else { return }
URLSession.shared.dataTask(with: url) { ( data, response, err ) in
DispatchQueue.main.sync {
if let err = err {
print("Failed to get data from url:", err)
return
}
guard let data = data else { return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let city = try decoder.decode(WeatherData.self, from: data)
self.weatherData.city = city.name
} catch {
print(error)
self.cityLabel.text = "Connection issues"
}
}
}.resume()
}
The error I am having is the line Cannot convert the value of type '[String: Any]' to expected argument type 'String'
in the line guard let url = URL(string: JsonURLString) else { return }
. I am wondering if I have gone down a rabbit hole and maybe use an alternative approach. I wanted to use codable in Swift 4 as this is supposed to be an easier way of parsing JSON. Now in this example, I am not sure. Any help would be much appreciated.
Upvotes: 1
Views: 7277
Reputation: 1222
Try this code.
JsonURLString is a [String: Any] type object not a String type. So you nee to convert it to a String type object.
private func getWeatherData(url: String, parameters: [String : String]) {
let JsonURLString:[String: Any] = ["url": WEATHER_URL, "parameters": parameters]
print(JsonURLString)
let urlString = JsonURLString["url"] as? String
guard let url = URL(string: urlString!) else { return }
URLSession.shared.dataTask(with: url) { ( data, response, err ) in
DispatchQueue.main.sync {
if let err = err {
print("Failed to get data from url:", err)
return
}
guard let data = data else { return }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let city = try decoder.decode(WeatherData.self, from: data)
self.weatherData.city = city.name
} catch {
print(error)
self.cityLabel.text = "Connection issues"
}
}
}.resume()
}
Upvotes: 1