为什么我不能为 Measurement<UnitMass> 变量赋值?

Why can't I assign a value to a Measurement<UnitMass> variable?

我很难弄清楚如何将计算结果分配给变量,fileprivate var oneRepMax: Measurement<UnitMass>?如您所见,它是一个可选的,在我尝试分配一个值后它只是保持为 nil

我最初尝试直接赋值给 .value 属性 像这样:

oneRepMax?.value = liftWeight * (Double(repetitions) ^^ 0.10)

但这没有用。然后我确定我需要在分配期间指定 unit (基于当前用户默认单位)所以我尝试了这个(这里是完整的上下文):

func calculateOneRepMax() -> Measurement<UnitMass> {

 let defaultWeightUnit = UserDefaults.weightUnit().unitName <-- returns "kg"
 let unit = UnitMass(symbol: defaultWeightUnit) <-- 'po unit' shows <NSUnitMass: 0x61000003ffa0> kg
 switch withFormula {        
     case "Lombardi":
        let result = liftWeight * (Double(repetitions) ^^ 0.10)
        oneRepMax? = Measurement(value: result, unit: unit)
     *case next case...*
}

我真的认为这行得通,但 oneRepMax 仍然为零。

我通读了 Apple 的文档,甚至还查看了一些 Ray Wenderlich 的截屏视频,但还没有找到答案。提前感谢您提供的任何帮助。

改变这个:

oneRepMax? = Measurement(value: result, unit: unit)

至:

oneRepMax = Measurement(value: result, unit: unit)

如果 oneRepMax 是当前具有 nil 值的可选值,则当您使用 oneRepMax? = ....

时不会发生赋值

的答案是相关的,并提供了更多解释。