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

时间:2017-07-29 22:37:31

标签: swift measurement

我的功能用于转换千克和磅。由于某些未知原因,converted(to:)方法似乎返回0(我认为不太可能)或将返回值分配给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分配给可选项,但我在此方法中没有任何可选值,所以我假设我必须做错了。

LiftEvent:

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的生成方式(我最初应该包含在我的问题中)。它是一个计算属性,我现在正在了解导致转换总是返回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
}

1 个答案:

答案 0 :(得分:2)

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

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