Last active
July 4, 2019 08:45
-
-
Save bpisano/a613ed407f052fab3fb0d3ff7afb9ac1 to your computer and use it in GitHub Desktop.
Embedded code in my Weather article on Medium
This file contains hidden or 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
| import SwiftUI | |
| import Combine | |
| class City: BindableObject { | |
| var didChange = PassthroughSubject<City, Never>() | |
| let name: String | |
| var weather: Weather? { | |
| didSet { | |
| didChange.send(self) | |
| } | |
| } | |
| init(name: String) { | |
| self.name = name | |
| self.getWeather() | |
| } | |
| private func getWeather() { | |
| guard let url = URL(string: WeatherManager.baseURL + "45.572353,5.915807?units=ca&lang=fr") else { | |
| return | |
| } | |
| URLSession.shared.dataTask(with: url) { (data, response, error) in | |
| guard let data = data else { | |
| return | |
| } | |
| do { | |
| let decoder = JSONDecoder() | |
| decoder.dateDecodingStrategy = .secondsSince1970 | |
| let weatherObject = try decoder.decode(Weather.self, from: data) | |
| DispatchQueue.main.async { | |
| self.weather = weatherObject | |
| } | |
| } catch { | |
| print(error.localizedDescription) | |
| } | |
| }.resume() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment