我有三个度量单位,我需要在它们之间进行转换,其中两个不是由Foundation提供的。Foundation提供的其中一个单元是UnitDispersion.partsPerMillion。另外两个单位是.millequivalentsPerLiter和degreesOfCarbonateHardness。
我需要使用的数学如下:
1 meq/L = 2.8 dKH = 50 ppm
我尝试过子类Foundation.UnitConverter,但是我不知道如何使用baseUnitValue(fromValue value: Double) -> Double和value(fromBaseUnitValue baseUnitValue: Double) -> Double来创建正确的结果。
我需要子类Foundation.UnitConverter还是Foundation.UnitConverterLinear?是否需要为每个单元之间的转换创建一个UnitConverter子类?
发布于 2019-05-16 16:06:05
作为一项规则,您不需要子类UnitConverter。相反,您可以创建UnitConverterLinear的具体实例。例如:
extension UnitDispersion {
static let millequivalentsPerLiter =
UnitDispersion(symbol: "meq/L",
converter: UnitConverterLinear(coefficient: 50))
}
let value = Measurement(value: 1,
unit: UnitDispersion.millequivalentsPerLiter) // 1.0 meq/L
value.converted(to: .partsPerMillion) // 50.0 ppmhttps://stackoverflow.com/questions/56172256
复制相似问题