iosswiftuser-experiencensnumberformattermeasurementformatter

Zero symbol in iOS MeasurementFormatter


I'm having problems to declare/use a zero symbol for an unknown value when using MeasurementFormatter:

let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .decimal
numberFormatter.zeroSymbol = "?"

numberFormatter.string(from: 0.0) // '?'

let formatter = MeasurementFormatter()
formatter.unitOptions = .providedUnit
formatter.numberFormatter = numberFormatter

var distance = Measurement<UnitLength>(value: 0, unit: .parsecs)
formatter.string(from: distance) // '0 pc' - expected: '? pc'

Trying different declarations of the value such as Double.zero doesn't change the output. Is this a conceptual thing in iOS or am I missing something here?


Solution

  • It turned out to produce the desired output by changing the Measurement declaration (distance):

    let dist1 = Measurement<UnitLength>(value: 0, unit: .parsecs) // output: '0 pc'
    let dist2 = Measurement(value: 0, unit: Unit(symbol: UnitLength.parsecs.symbol)) // output '? pc' as expected
    

    A radar is filed.