Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created March 18, 2019 15:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansemannn/0865b1ceb7fa7e13b9908ece128325be to your computer and use it in GitHub Desktop.
Save hansemannn/0865b1ceb7fa7e13b9908ece128325be to your computer and use it in GitHub Desktop.
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