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

321 views Asked by At

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

2

There are 2 answers

0
lorem ipsum On BEST ANSWER

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

0
Aakash Bondwal On

try using [DayWeather]

updated code will be: Text("L: (weather.dailyForecase[0].lowTemperature) H: (weather.dailyForecast[0].highTemperature)")

here [0] is the first index of the array items in DayWeather