我已经实现了一个 NSMeasurementFormatter 并且遇到了一个奇怪的问题。当应用程序加载为 en_US 时,我将所有的重量以磅的形式加载到文本框中,太棒了。但是,当我将应用程序切换到 en_GB 并加载相同的数据时,文本框中没有任何内容。

当我打印对象时,打印输出以公斤为单位显示重量,因此它们可以正确转换,但它们没有加载到用户可见的实际文本框中。

这有什么明确的原因吗?我花了几个小时试图弄清楚为什么它适用于一个位置而不适用于另一个!欣赏洞察力,这里是代码:

这是练习的初始保存,根据此时的应用程序设置,它以一种方式或另一种方式进行

        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
}

因此,当设置为 US 时,我会得到 2 个小数位的磅读数,但是当我设置为 GB 时,我什么也看不到,我只能看到它在对象的打印输出中进行了转换,控制台下方的图片显示用于加载例程和结果截图:

ios - NSMeasurementFormatter 显示英制重量但不显示公制重量?-LMLPHP

ios - NSMeasurementFormatter 显示英制重量但不显示公制重量?-LMLPHP

ios - NSMeasurementFormatter 显示英制重量但不显示公制重量?-LMLPHP

在下面更新了 MASSFORMATTER 信息
    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))
    }
}

}

最佳答案

这一切都归结为以下代码:

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))
}

关于ios - NSMeasurementFormatter 显示英制重量但不显示公制重量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42142586/

10-10 21:48