How to find out if the precipitationAmount from WeatherKit is in millimeters?

103 views Asked by At

I'd like to find out if the precipitationAmount is in .millimeters or something else but the code below don't works. Do you know why?

let dWeather: Forecast<DayWeather>

if dWeather.first?.precipitationAmount.unit == .millimeters {
    Text("Millimeters")
}
if dWeather.first?.precipitationAmount.unit == .inches {
    Text("Inches")
}
if dWeather.first?.precipitationAmount.unit != .millimeters && dWeather.first?.precipitationAmount.unit != .inches {
    Text("Something else")
}

The output in en_UK and fr_FR should be .millimeters and in en_US should be .inches but I always get Millimeters.

1

There are 1 answers

2
lorem ipsum On

SwiftUI as a feature implements localization my default when you use standard formatters such as.

 .formatted(.measurement(width: .abbreviated, usage: .rainfall, numberFormatStyle: .number.precision(.fractionLength(2))))

But if you change .rainfall to .asProvided you can see the actual unit from the API.

.formatted(.measurement(width: .abbreviated, usage: .asProvided, numberFormatStyle: .number.precision(.fractionLength(2))))

Apple does not specify a unit in the documentation so I would not assume a unit.

/// The amount of precipitation for the hour.
///
/// This value refers to the liquid equivalent of all precipitation amounts.
/// Note that since this is the amount over the hour and the precipitation intensity is also amount over the hour,
/// this property also means precipitationIntensity
public var precipitationAmount: Measurement<UnitLength>