Created
August 12, 2021 13:25
-
-
Save thatswiftguy/dcb4bec0215dde36dedfc27f1161190e to your computer and use it in GitHub Desktop.
Enjoy !
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
class ViewModel : ObservableObject { | |
@Published var weatherData : WeatherResponse? | |
@Published var showErrorAlert = false | |
@Published var alertDescription = "" | |
let city = "https://api.openweathermap.org/data/2.5/weather?q=delhi&appid={YOUR-API-KEY}&units=metric" | |
init(){ | |
WeatherService.getData(city: city) { result in | |
switch result { | |
case .success(let data): | |
self.weatherData = data | |
case .failure(let error): | |
if error == .cityNotFound { | |
self.showErrorAlert = true | |
s🔥elf.alertDescription = "City Not Found" | |
} else if error == .timeOut { | |
self.showErrorAlert = true | |
self.alertDescription = "Network Time Out" | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment