Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- func handleData() {
- let jsonData: [String: Any]
- let text = String(describing: self.data)
- // JSON serialisation can fail, use do-catch (like try-catch in
- // to cope with possible failuresJava/C)
- do {
- // JSON data is copied into a key/value dictionary
- jsonData = try JSONSerialization.jsonObject(with: self.data, options: [.mutableContainers]) as! [String: Any]
- } catch {
- return debugPrint("Error occurred: \(error.localizedDescription)")
- }
- // Extract the core element main
- guard let main = jsonData["main"] as? [String: Any] else { return }
- // Interpret the JSON elements temp as Double value
- guard let tempKelvin = main["temp"] as? Double else { return }
- // Calculate Kelvin into Celsius
- let tempCelsius = tempKelvin - 273.15
- // Copy the temperature into the corresponding UI label
- self.temperatureLabel.text = String(format: "%.1f °C", tempCelsius) as String
- // Copy description text into the corresponding label of the UI
- self.descriptionLabel.text = description
- // Same for the city name
- guard let name = jsonData["name"] as? String else { return }
- self.cityLabel.text = name
- // next step show location in map
- // handleMap()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement