Home > front end >  WeatherKit APPLE IOS 16 - Low/Hight temperature values
WeatherKit APPLE IOS 16 - Low/Hight temperature values

Time:01-19

I would like to create a weather application with the Apple weatherKit. I would like to create a gauge to display the current temperature between the lowest temperature and the highest.

I can't get the maximum and minimum temperatures.

https://developer.apple.com/documentation/weatherkit/dayweather/hightemperature

@State private var weather: Weather?

var daily: [DayWeather] {
    if let weather {
        return Array(weather.dailyForecast.filter { daily in
            return daily.date.timeIntervalSince(Date()) >= 0
        })
    } else {
        return []
    }
}

Gauge(value: temperature.doubleValue() ?? 0, in: daily. lowTemperature... daily.highTemperature) {
}.gaugeStyle(.accessoryLinear)`

CodePudding user response:

The error

Value of type '[DayWeather]' has no member 'lowTemperature'

tells you that daily contains multiple items. I assume that you want to display the current temperature between the lowest temperature and the highest of today.

if so change the computed property to

var todayForecast: DayWeather? {
   weather?.dailyForecast.first{ Calendar.current.isDateInToday($0.date) }
}

which returns the DayWeather object of today, if available

Then you can write

if let todayForecast {
    Gauge(value: temperature.value, 
          in: todayForecast.lowTemperature.value...todayForecast.highTemperature.value) {
    }.gaugeStyle(.accessoryLinear)
}
  • Related