How to Fetch SunEvents?

Hello, I'm quite new to SwiftUI Development, learned how to code without getting a Compute Science college Degree. Please advise how do I fetch SunEvents?

private func showWeatherInformation(latitude: Double, longtitude: Double) async {

        let weatherService = WeatherService()

        let journalingWeather = CLLocation(latitude: latitude, longitude: longtitude)

        

        do {

            let weather = try await weatherService.weather(for: journalingWeather)

            let temperature = weather.currentWeather.temperature.value.description

            let humidity = weather.currentWeather.humidity.formatted(.percent).description

            let weatherDesc = weather.currentWeather.condition.description

            let weatherIcon = weather.currentWeather.symbolName.description

            let feltLikeTemp = weather.currentWeather.apparentTemperature.value.description

            let pressure = weather.currentWeather.pressure.value.description

            let dewPoint = weather.currentWeather.dewPoint.value.description

            let uvIndex = weather.currentWeather.uvIndex.value.description

            let visibility = round(weather.currentWeather.visibility.value).description

            let windSpeed = weather.currentWeather.wind.speed.value.description

            let compass = weather.currentWeather.wind.direction.value.description

            

            /// What to decode?

            let sunEvents = try SunEvents(from: journalingWeather as! Decoder)

            let sunrise = sunEvents.sunrise ?? Date.now

            let sunset = sunEvents.sunset ?? Date.now

            

            self.temperatureInput = temperature

            self.weatherDescriptionInput = weatherDesc

            self.weatherIconInput = weatherIcon

            self.feltLikeTemperatureInput = feltLikeTemp

            self.pressureInput = pressure

            self.humidityInput = humidity

            self.dewPointInput = dewPoint

            self.uvIndexInput = uvIndex

            self.visibilityInput = visibility

            self.windSpeedInput = windSpeed

            self.windDegreeInput = compass

            self.sunriseTimeInput = sunrise

            self.sunsetTimeInput = sunset

        } catch {

            print("\(error)")

        }

    }

I'm getting this error Thread 1: signal SIGABRT at let sunEvents = try SunEvents(from: journalingWeather as! Decoder) when I run it on actual device.

Any help would be very much appreciated. Thank you!

Answered by Engineer in 724865022

Hi @kaviraja,

The weather you get back from weatherService.weather(for:) is of type Weather. Here are the docs: https://developer.apple.com/documentation/weatherkit/weather. You're accessing currentWeather, which is of CurrentWeather type (see docs), but SunEvents is only returned from DayWeather (see docs), which you can find via weather.dailyForecast[0].sun.

Hope that helps.

Thank you for the reply @Jineshsethia, I'm only fetching the current date sunEvents and the latitude and longitude details are available in let journalingWeather = CLLocation(latitude: latitude, longitude: longtitude). Could you advise how to use the decoder?

Accepted Answer

Hi @kaviraja,

The weather you get back from weatherService.weather(for:) is of type Weather. Here are the docs: https://developer.apple.com/documentation/weatherkit/weather. You're accessing currentWeather, which is of CurrentWeather type (see docs), but SunEvents is only returned from DayWeather (see docs), which you can find via weather.dailyForecast[0].sun.

Hope that helps.

Thank you very much! Ya, it works now!

How to Fetch SunEvents?
 
 
Q