我很难弄清楚如何将计算结果赋值给一个变量,如您所见,fileprivate var oneRepMax: Measurement<UnitMass>?是一个可选的,在我尝试给它赋值之后,它只是保持为零。
我最初试图对.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是零。
我已经阅读了苹果的文档,甚至审查了雷·温德利希的一些屏幕,但还没有找到答案。谢谢你能提供的任何帮助。
发布于 2017-04-25 14:32:31
改变这一点:
oneRepMax? = Measurement(value: result, unit: unit)至:
oneRepMax = Measurement(value: result, unit: unit)如果oneRepMax是当前带有nil值的可选值,则在使用oneRepMax? = ...时不会发生分配。
iOS/Swift: Can't assign optional String to UILabel text property的答案是相关的,并提供了更多的解释。
https://stackoverflow.com/questions/43613421
复制相似问题