Thank you for your feedback. Unfortunately, this does not solve my problem. The function is supposed to be applicable for all possible (like UnitArea, UnitAcceleration, UnitAngle, ...).
I wrote the following function which returns me the desired result as string.
func showMeasurement(value: Double, unit: String, symbol: Bool) -> String {
switch(unit) {
//MARK: Aceleration
case "UnitAcceleration.metersPerSecondSquared":
let newValue = Measurement(value: value, unit: UnitAcceleration.metersPerSecondSquared)
if symbol {
return newValue.unit.symbol
} else {
return newValue.formatted(.measurement(width: .narrow, usage: .asProvided, numberFormatStyle: .number.precision(.fractionLength(2))))
}
case "UnitAcceleration.UnitAcceleration":
let newValue = Measurement(value: value, unit: UnitAcceleration.baseUnit())
if symbol {
return newValue.unit.symbol
} else {
return newValue.formatted(.measurement(width: .narrow, usage: .asProvided, numberFormatStyle: .number.precision(.fractionLength(2))))
}
....
But later I will also need the corresponding Measurment to be able to calculate with the values.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: