为什么我的 kg/lb 转换返回 0?

Why is my kg/lb conversion returning 0?

我的函数是用来在公斤和磅之间进行转换的。由于某些未知原因,converted(to:) 方法似乎要么是 returning 0(我认为这不太可能),要么是 returned 值分配给 liftWeight 不是没有工作。

func convertUnit(liftEvent: LiftEventRepresentable ) -> LiftEventRepresentable {
    let unit = liftEvent.liftWeight.unit.symbol
    switch unit {
    case "kg":
        let weight = liftEvent.liftWeight
        print("Event weight is \(weight)") // 110.0 kg
        let liftWeight = weight.converted(to: .pounds) // this is returning 0 or the assignment isn't working
        print("The new weight is \(liftWeight)") 0.0 lb
        liftEvent.updateWeightLifted(liftWeight.value)
        liftEvent.updateWeightUnit(liftEvent.liftWeight.unit.symbol)
        return liftEvent
    case "lb":

        // I'll finish this case after I get case "kg": working

    default:
        fatalError("Oops, this should not happen")
    }
}

我认为这可能与我过去遇到的问题类似,该问题涉及尝试将 Measurement 分配给可选值,但我在此方法中没有任何可选值,所以我假设我必须做错事了。

提升事件:

extension LiftEvent {
    @NSManaged var date: Date
    @NSManaged var repetitions: NSNumber
    @NSManaged var uid: NSNumber?
    @NSManaged var weightLifted: NSNumber
    @NSManaged var weightUnit: String
    @NSManaged var formula: Formula
    @NSManaged var lift: Lift
}

LiftEventRepresentable 协议是:

@objc protocol LiftEventRepresentable {
    var date: Date { get }
    var formula: Formula { get }
    var repetitions: NSNumber { get set }
    var liftWeight: Measurement<UnitMass> { get }
    var maxWeight: Double { get }
    var lift: Lift { get }
    var context: NSManagedObjectContext { get }
    var userDidChangeUnits: Bool { get set }

    func updateWeightLifted(_ weight: Double)
    func updateRepetitions(_ repetitions: Int)
    func updateMaxAmount(_ maxAmount: Measurement<UnitMass>) -> ()
    func updateLiftEventFormula(_ newFormulaUuid: String) -> ()
    func updateLift(_ newLiftUuid: String) -> ()
    func updateWeightUnit(_ newUnit: String)
    func calculateOneRepMax() -> Measurement<UnitMass>
    func updateLiftEventWithFinalValues()
}

谁能告诉我为什么 liftEvent.liftWeight 转换后是 0.0 磅?

编辑 1:这就是 liftEvent.liftWeight 的产生方式(我原本应该将其包含在我的问题中)。这是一个计算的 属性,我现在知道它是导致转换总是 return 0):

的原因
class LiftEvent: NSManagedObject, LiftEventRepresentable {

    var liftWeight: Measurement<UnitMass> {
        let defaultUnit = UserDefaults.weightUnit() // returns a string ("kg" or "lb")
        let unitSymbol = UnitMass.init(symbol: defaultUnit) // the wrong way
        let liftWeight = Measurement<UnitMass>(value: Double(weightLifted), unit: unitSymbol)
    return liftWeight
    }

编辑 2:根本问题确实是 liftEvent.liftWeight 无效。

这是我现在可以使用的新代码:

var liftWeight: Measurement<UnitMass> {
    let defaultUnit = UserDefaults.weightUnit() // returns a string ("kg" or "lb")
    let unit: UnitMass
    if defaultUnit == "kg" {
        unit = UnitMass.kilograms // the right way
        } else {
        unit = UnitMass.pounds // also the right way
    }
    let liftWeight = Measurement<UnitMass>(value: Double(weightLifted), unit: unit)
    return liftWeight
}

Measurement 的设备出了点问题。它是一个 UnitMass,因此它具有正确的维度,但它的转换器丢失了。这可能是因为您使用 UnitMass(symbol: "kg") 而不是 UnitMass.kilograms.

创建它
// right way
let goodUnit = UnitMass.kilograms
let goodWeight:Measurement = Measurement(value: 110.0, unit: goodUnit)
print("Good pounds: ", goodWeight.converted(to: .pounds)) // 242.508686220216 lb

// wrong way (results in 0.0 output)
let badUnit = UnitMass(symbol: "kg")
let badWeight:Measurement = Measurement(value: 110.0, unit: badUnit)
print("Bad pounds: ", badWeight.converted(to: .pounds)) // 0.0 lb

UnitMass(symbol: "kg")创建一个单元实际上是有效的代码,所以没有错误。您可以使用此语法使用您想要的任何符号和自定义转换器创建您自己的质量单位。由于某些原因,converter: 参数是可选的,因此 UnitMass(symbol: "kg") 只是使用符号 "kg" 和默认的 UnitLinearConverter 系数为 0.

改用 UnitMass.kilograms 可以为您提供一个预定义的千克单位,​​该单位已经设置了正确的符号和换算器。很容易犯错误,特别是因为没有错误。

我建议您查看创建 liftEvent.liftWeight 的位置,因为这很可能就是问题所在。