如何在 SwiftUI 中从 WeatherKit 获取最低和最高温度?

问题描述 投票:0回答:2

我试图从Apple的WeatherKit获取最低和最高温度,以使我的天气应用程序变得复杂,但Apple的开发人员文档没有显示如何获取该值,并且以前没有人问过这个问题。

这是我尝试过但不起作用的代码:

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

这是我所期望的(我对此进行了硬编码):

swiftui temperature weatherkit
2个回答
1
投票

您可以通过

DayWeather

获得它

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

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

您可以通过

.daily

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

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


0
投票

尝试使用[DayWeather]

更新后的代码将是: Text("L: (weather.dailyForecase[0].lowTemperature) H: (weather.dailyForecast[0].highTemperature)")

这里 [0] 是 DayWeather 中数组项的第一个索引

© www.soinside.com 2019 - 2024. All rights reserved.