Created
July 31, 2021 10:40
-
-
Save thatswiftguy/60b55507ea799614621e0c2f80799478 to your computer and use it in GitHub Desktop.
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 temp : Double | |
init(){ | |
temp = 0 | |
getData() | |
} | |
func getData() { | |
guard let url = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=delhi&appid={YOUR-API-KEY}&units=metric") else { | |
return | |
} | |
var request = URLRequest(url: url) | |
request.httpMethod = "GET" | |
URLSession.shared.dataTask(with: request) { data, response, error in | |
guard data != nil else { | |
print("data is nil") | |
return | |
} | |
let decoder = JSONDecoder() | |
let decodedData = try? decoder.decode(WeatherResponse.self, from: data!) | |
DispatchQueue.main.async { | |
self.temp = (decodedData?.main.temp)! | |
} | |
}.resume() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment