0
是否有可能在swift中將自定義unitOptions添加到MeasurementFormatter?我想能夠定義的,如.imperial和.metric在Swift中爲MeasurementFormatter添加額外的unitOptions
extension UnitEnergy {
static let footPounds = UnitEnergy(symbol: "ft-lbs", converter: UnitConverterLinear(coefficient: 1))
}
var test = Measurement<UnitEnergy>(value: 10, unit: .footPounds)
var formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "es")
print(formatter.unitOptions)
print(formatter.string(from: test))
formatter.unitOptions = .naturalScale
print(formatter.unitOptions)
print(formatter.string(from: test))
formatter.unitOptions = .providedUnit
print(formatter.unitOptions)
print(formatter.string(from: test))
輸出:
UnitOptions(rawValue: 0)
10 J
UnitOptions(rawValue: 2)
2,39 cal
UnitOptions(rawValue: 1)
10 ft-lbs
即使您向枚舉中添加更多選項,MeasurementFormatter也不會知道任何有關新值的信息。 – rmaddy
那麼是否有一種方法可以覆蓋繼承自MeasurementFormatter的可以處理增加值的類中的函數? – Andrew