当我在Playground中运行以下代码时,格式化的字符串返回为零。我在派生的自定义Measurement类中缺少什么?在派生单元中使用MeasurementFormatter
open class UnitFlowRate : Dimension {
open override static func baseUnit() -> UnitFlowRate { return self.metricTonsPerHour }
static let shortTonsPerHour = UnitFlowRate(symbol: NSLocalizedString("stph", comment: "short tons per hour"), converter: UnitConverterLinear(coefficient: 1))
static let metricTonsPerHour = UnitFlowRate(symbol: NSLocalizedString("mtph", comment: "metric tons per hour"), converter: UnitConverterLinear(coefficient: 2))
}
var measureCustom = Measurement<UnitFlowRate>(value: 12.31, unit: .shortTonsPerHour)
var measureSystem = Measurement<UnitLength>(value: 12.31, unit: .inches)
var formatter = MeasurementFormatter()
var measureStringCustom = formatter.string(for: measureCustom)
var measureStringSystem = formatter.string(for: measureSystem)
print(measureCustom) // This works
print(measureSystem) // This works
print(measureStringCustom) // This is nil - Why?
print(measureStringSystem) // This works
输出:
12.31 stph
12.31 in
nil
Optional("0 mi")