iOS MeasurementFormatter 中的零符号
Zero symbol in iOS MeasurementFormatter
我在使用 MeasurementFormatter
时遇到了 declare/use 未知值零符号的问题:
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'
尝试对值进行不同的声明,例如 Double.zero
不会更改输出。
这是 iOS 中的概念性事物还是我在这里遗漏了什么?
It turned out 通过更改测量声明 (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
一个radar被归档了。
我在使用 MeasurementFormatter
时遇到了 declare/use 未知值零符号的问题:
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'
尝试对值进行不同的声明,例如 Double.zero
不会更改输出。
这是 iOS 中的概念性事物还是我在这里遗漏了什么?
It turned out 通过更改测量声明 (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
一个radar被归档了。