Skip to content

Instantly share code, notes, and snippets.

@myawesomehub
Created August 12, 2021 13:25
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 myawesomehub/dcb4bec0215dde36dedfc27f1161190e to your computer and use it in GitHub Desktop.
Save myawesomehub/dcb4bec0215dde36dedfc27f1161190e to your computer and use it in GitHub Desktop.
Enjoy !
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