How to convert between volumetric HKUnits?
Solution 1:
According to the link you provided, under Working With Units:
var value = HKQuantity(unit: .fluidOunceUS(), doubleValue: 42)
var convertedValue = value.doubleValue(for: .liter())
https://developer.apple.com/documentation/healthkit/hkquantity/1615245-doublevalue
Solution 2:
Foundation includes a Measurement
structure that allows for a wide variety of unit conversions. For example, to convert from fluid ounces to liters:
let fluidOz = 32.0
let liters = Measurement(value: fluidOz, unit: UnitVolume.fluidOunces).converted(to: .liters).value
print(liters) // 0.946352
let degrees = 45.0
let radians = Measurement(value: degrees, unit: UnitAngle.degrees).converted(to: .radians).value
print(radians) // 0.7853981633974483
In addition to UnitVolume
, there is also UnitSpeed
, UnitMass
, and many more.
https://developer.apple.com/documentation/foundation/measurement