0
신속하게 MeasurementFormatter에 custom unitOptions를 추가 할 수 있습니까? 나는 그런 .imperial 및 .metricSwift에서 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