我已经实现了一个NSMeasurementFormatter并且遇到了一个奇怪的问题。当应用程序以en_US加载时,我将所有权重加载到文本框中,以磅为单位。但是,当我将我的应用程序切换到en_GB并加载相同的数据时,文本框中没有显示任何内容。
当我打印对象时,打印输出以kg为单位表示权重,以便它们正确转换,但它们不会加载到实际文本框中以便用户可见。
这有什么明确的原因吗?我花了几个小时试图找出它为什么适用于一个位置而不是另一个位置!欣赏洞察力,这里是代码:
以下是练习的初始保存,根据此时的应用设置,它可以采用另一种方式
if self.localeIdentifier == "en_GB" {
let kgWeight = Measurement(value: Double(self.userExerciseWeight.text!)!, unit: UnitMass.kilograms)
newUserExercise.weight = kgWeight as NSObject?
newUserExercise.initialMetricSystem = self.localeIdentifier
print("SAVED AS \(localeIdentifier) METRIC")
} else if self.localeIdentifier == "en_US" {
let lbsWeight = Measurement(value: Double(self.userExerciseWeight.text!)!, unit: UnitMass.pounds)
newUserExercise.weight = lbsWeight as NSObject?
newUserExercise.initialMetricSystem = self.localeIdentifier
print("SAVED AS \(localeIdentifier) IMPERIAL")
}
然后我尝试稍后重新加载此对象权重属性
let formatter = MeasurementFormatter()
let exerciseWeight = userExercise.weight as! Measurement<Unit>
let localeIdentifier = UserDefaults.standard.object(forKey: "locale")
let locale = Locale(identifier: localeIdentifier as! String)
formatter.locale = locale
let numberFormatter = NumberFormatter()
numberFormatter.maximumFractionDigits = 2
formatter.numberFormatter = numberFormatter
let finalWeight = formatter.string(from: exerciseWeight)
cell.todaysExerciseWeightLabel.text = finalWeight
}
因此,当设置为美国时,我得到磅读数到2位小数,但是当我设置为GB时我什么也看不见,我只能看到它在对象的打印中进行了转换,下面的图片是显示加载两个例程的控制台,以及结果的镜头:
更新以下MASSFORMATTER INFO
let localeIdentifier = UserDefaults.standard.object(forKey: "locale") as! Locale
let exerciseWeight = userExercise.weight as! Measurement<Unit>
let formatter = MassFormatter()
formatter.numberFormatter.locale = localeIdentifier
formatter.numberFormatter.maximumFractionDigits = 2
if localeIdentifier.usesMetricSystem {
let kgWeight = exerciseWeight.converted(to: .kilograms)
let finalKgWeight = formatter.string(fromValue: kgWeight.value, unit: .kilogram)
cell.todaysExerciseWeightLabel.text = finalKgWeight
print(formatter.string(fromValue: kgWeight.value, unit: .kilogram))
} else {
let kgWeight = exerciseWeight.converted(to: .pounds)
let finalLbWeight = formatter.string(fromValue: exerciseWeight.value, unit: .pound)
cell.todaysExerciseWeightLabel.text = finalLbWeight
print(formatter.string(fromValue: exerciseWeight.value, unit: .pound))
}
}
}
答案 0 :(得分:2)
这一切归结为以下代码:
let weight = Measurement(value: 10.0, unit: UnitMass.kilograms)
let formatter = MeasurementFormatter()
formatter.locale = Locale(identifier: "en_GB")
print(formatter.string(from: weight))
结果为空String
(""
)。
这是一个明显的错误。请报告。
一个简单的解决方法是使用较旧的MassFormatter
:
//let locale = Locale(identifier: "en_GB")
let locale = Locale(identifier: "en_US")
let weight = Measurement(value: 10.24, unit: UnitMass.pounds)
let formatter = MassFormatter()
formatter.numberFormatter.locale = locale
formatter.numberFormatter.maximumFractionDigits = 2
if locale.usesMetricSystem {
let kgWeight = weight.converted(to: .kilograms)
print(formatter.string(fromValue: kgWeight.value, unit: .kilogram))
} else {
print(formatter.string(fromValue: weight.value, unit: .pound))
}