Incidentally, the WeatherKit REST API includes "precipitationAmount" for hourly weather conditions:
https://developer.apple.com/documentation/weatherkitrestapi/hourweatherconditions
so it seems odd and inconsistent that it is not included in the Structure definition for HourWeather:
https://developer.apple.com/documentation/weatherkit/hourweather
Post
Replies
Boosts
Views
Activity
So I've solved my own issue for now. It may not be pretty (haven't used strings much), but by defining this function I was able to extract the precipitation amount as a Measurement, just like other elements of the HourWeather struct.
// Function to get precipitationAmount as a measurement from one instance of HourWeather
// Written by Neil Gordon, 14 June 2022 - pending inclusion of precipitationAmount in the
// WeatherKit definition of the Structure for HourWeather
func precipitationAmount(hourWeatherElement: HourWeather) -> Measurement<UnitLength> {
var contents : String = " "
var answer = Measurement(value: -9.9, unit: UnitLength.millimeters) // Default if problems
print (hourWeatherElement, to: &contents) // Get the contents of the instance as a string
if let firstRange = contents.range(of: "precipitationAmount: ") { // Find where precip amount should be
let startIndex = firstRange.upperBound
// Drop everything before that
contents = String(contents[startIndex...])
// Find position of first comma, then use up to just before that
let commaIndex = contents.firstIndex(of: ",")!
// Get the two bits of value and units - usually looks something like "2.3 mm"
let contents = contents[..<commaIndex]
let bits = contents.components(separatedBy: " ")
// Convert string to a Double for use in Measurement
if let amount = Double(bits[0]) {
let units = bits[1]
// I think mm and in are all I should see?
if units == "mm" {
answer = Measurement(value: amount, unit: UnitLength.millimeters)
} else {
answer = Measurement(value: amount, unit: UnitLength.inches)
}
}
}
return answer
}