Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Created January 26, 2023 19:49
Show Gist options
  • Save BasThomas/f91bf58c7015c2295b47130a1a7a7bcc to your computer and use it in GitHub Desktop.
Save BasThomas/f91bf58c7015c2295b47130a1a7a7bcc to your computer and use it in GitHub Desktop.
extension Locale {
var defaultTemperatureUnit: UnitTemperature {
if #available(iOS 16, *) {
return UnitTemperature(forLocale: self, usage: .general)
} else {
let mCel = Measurement(value: 0, unit: UnitTemperature.celsius)
let f = MeasurementFormatter()
f.locale = self
let s1 = f.string(from: mCel)
f.unitOptions = .providedUnit
let s2 = f.string(from: mCel)
return s1 == s2 ? .celsius : .fahrenheit
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment