Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. func handleData() {
  2. let jsonData: [String: Any]
  3. let text = String(describing: self.data)
  4. // JSON serialisation can fail, use do-catch (like try-catch in
  5. // to cope with possible failuresJava/C)
  6. do {
  7. // JSON data is copied into a key/value dictionary
  8. jsonData = try JSONSerialization.jsonObject(with: self.data, options: [.mutableContainers]) as! [String: Any]
  9. } catch {
  10. return debugPrint("Error occurred: \(error.localizedDescription)")
  11. }
  12.  
  13. // Extract the core element main
  14. guard let main = jsonData["main"] as? [String: Any] else { return }
  15. // Interpret the JSON elements temp as Double value
  16. guard let tempKelvin = main["temp"] as? Double else { return }
  17. // Calculate Kelvin into Celsius
  18. let tempCelsius = tempKelvin - 273.15
  19.  
  20. // Copy the temperature into the corresponding UI label
  21. self.temperatureLabel.text = String(format: "%.1f °C", tempCelsius) as String
  22.  
  23. // Copy description text into the corresponding label of the UI
  24. self.descriptionLabel.text = description
  25.  
  26. // Same for the city name
  27. guard let name = jsonData["name"] as? String else { return }
  28. self.cityLabel.text = name
  29.  
  30. // next step show location in map
  31. // handleMap()
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement