Created
March 18, 2019 15:30
-
-
Save hansemannn/0865b1ceb7fa7e13b9908ece128325be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment