swiftuitemperatureweatherkit

How to get the Minimum and Maximum temperatures from WeatherKit in SwiftUI?


I am trying to get the Minimum and Maximum Temperatures from Apple's WeatherKit to make a complication in my Weather app but Apple's Developer Documentation does not show how to get the value and nobody has asked this question before.

Here is the code I tried but didn't work:

Text("L: \(weather.currentWeather.lowTemperature) H: \(weather.currentWeather.highTemperature)")
// Text("L: \(Minimum Temperature) H: \(Maximum Temperature)")

Here is what I expected(i hardcoded this):hardcoded image


Solution

  • You get it with DayWeather

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

    var highTemperature: Measurement<UnitTemperature>
    var lowTemperature: Measurement<UnitTemperature>
    

    You can get it with .daily

    let daily = try await service.weather(for: newYork, including: .daily)
    

    https://developer.apple.com/documentation/weatherkit/weatherquery