此软件包是对 Weather Underground API 的简单封装。 它提供对个人气象站 (PWS) 的当前值、以及每小时和每日历史记录的快速访问。 如果有兴趣,这些功能可能会在将来扩展(欢迎提交 PR!)。
该文档直接来自 Weather Underground API。
该封装器是一个纯 Swift 封装器,具有 codable 功能,并使用 Foundation 进行网络连接。 没有依赖项!
要使用 API,只需设置来自 Weather Underground 的 API 密钥,并像这样指定您的站点
WeatherGround.measure.apiKey = "1234567890af"
WeatherGround.measure.station = "station_id"
如果您想检索站点的当前值
WeatherGround.measure.current(){ values in
guard case .success(let observation) = values else {
print("Cannot retrieve the current values")
return
}
print("The weather is: \(observation)")
}
如果您需要检索历史值,您可以使用 hourly
、daily
或 all
中的一个。 hourly
和 all
返回观测列表,而 daily
返回每日平均值。
WeatherGround.measure.hourly(for: Date()){ history in
guard case .success(let observations) = history else {
print("Unable to retrieve the observations.")
return
}
print("The weather for \(Date()) was \(observations)")
}
5 天预报也可通过 forecast
获得。 它不需要 station
值,只需要 apiKey
。
WeatherGround.measure.forecast(for: Location.geo(latitude: 33.74, longitude: -84.39)){ fiveDay in
guard case .success(let forecast) = fiveDay else {
print("Unable to retrieve the forecast")
return
}
print("5 day forecast:")
for (day, narrative) in zip(forecast.dayOfWeek, forecast.narrative) {
print("\(day): \(narrative)")
}
if let daypart = forecast.daypart.first {
print("5 day forecast per day:")
for (day, narrative) in zip(daypart.daypartName, daypart.narrative) {
if let day = day, let narrative = narrative {
print("\(day): \(narrative)")
}
}
}
}
您还可以检索风向、强度和描述、以及温度、雨、雪、月相、日出和日落、雷暴和降水概率、湿度和图标 ID(列表和 ID 可在此处获得)