Trying to get weather data for a specific location and specific timezone. EDITS: Based on suggestions provided, I called WeatherService like this:
func getWeatherServiceData(requestId: String,
latitude: Double,
longitude: Double,
completion: @escaping (Bool) -> () ) async{
var hourlyForecast = HourlyForecast()
var hourlyWeather = HourlyWeather(hourlyForecast: [hourlyForecast])
var dailyForecast = DailyForecast()
var dailyWeather = DailyWeather(dailyForecast: [dailyForecast])
let weatherService = WeatherService()
let location = CLLocation(latitude: latitude, longitude: longitude)
let weather = try! await weatherService.weather(for: location)
let temperature = weather.currentWeather.temperature
let uvIndex = weather.currentWeather.uvIndex
let cnt = weather.hourlyForecast.count
for hourlyForecast in weather.hourlyForecast {
var hourly = HourlyForecast()
hourly.temp = hourlyForecast.temperature.converted(to: .fahrenheit).value
hourly.pressure = hourlyForecast.pressure.converted(to: .bars).value
hourly.date = hourlyForecast.date
hourlyWeather.hourlyForecast.append(hourly)
}
for dailyForecast in weather.dailyForecast {
var daily = DailyForecast()
daily.tempHigh = dailyForecast.highTemperature.converted(to: .fahrenheit).value
daily.tempLow = dailyForecast.lowTemperature.converted(to: .fahrenheit).value
daily.date = dailyForecast.date
daily.dayOfWeek = dailyForecast.date.dayOfWeek()
daily.precipitation = dailyForecast.precipitationAmount.converted(to: .inches).value
dailyWeather.dailyForecast.append(daily)
}
when I call this line:
let weather = try! await weatherService.weather(for: location)
I get error:
Thread 23: Fatal error: 'try!' expression unexpectedly raised an error: Error Domain=WeatherDaemon.WDSJWTAuthenticatorServiceListener.Errors Code=2 "(null)"
encountered an error when fetching weather data subset; location=<+29.92889404,-95.60774752> +/- 0.00m (speed -1.00 mps / course -1.00) @ 3/14/24, 11:21:14 AM Central Daylight Time, error=WeatherDaemon.WDSJWTAuthenticatorServiceListener.Errors 2 Error Domain=WeatherDaemon.WDSJWTAuthenticatorServiceListener.Errors Code=2 "(null)"