Playing with WeatherKit in Swiftui recently (very new to the language). I'm working on a simple weather app and I'm trying to achieve two things:
- Know (true or false) if will be raining on the current day;
- Weather conditions (symbol name and temperature) for the next hour (just the hour next to the current).
I'm already displaying the conditions for the current weather. Which is the best approach?
This is the function I'm using to get the current weather:
func getWeather() async {
do {
weather = try await Task.detached(priority: .userInitiated) {
return try await WeatherService.shared.weather(for: .init(latitude: 42.28851, longitude: 13.55401)) // Coordinates for SAN PIO just as example coordinates
}.value
} catch {
fatalError("\(error)")
}
}
I'm getting the "next hour" condition symbol (and converting it into a corresponding emoji) with:
var hourlyWeatherForecast: String {
let nextHourWeather = weather?.hourlyForecast.forecast.first?.condition.emoji
return nextHourWeather ?? "Waiting"
}
but struggling to check if rain (or drizzle, or thunderstorms, etc...) is expected for the current day.
Get
hourlyanddailyforecast.Get the forecast for today by extracting the information with
and check if the
conditionis rainy.trueget the forecast for the next hour by filtering thehourlyforecast similar to 2. but comparing thehourcomponentEdit:
To get also
dailyandhourlyforecast use theincludingparameter:You can check for you desired condition pretty simply. Inside the
WeatherConditionextension addAdd as many cases as you like in the
truebranch.