[英]Why can't I assign a value to a Measurement<UnitMass> variable?
我很難弄清楚如何將計算結果分配給一個變量, 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為零。
我已經閱讀了Apple的文檔,甚至閱讀了一些Ray Wenderlich的截屏視頻,但還沒有找到答案。 在此先感謝您提供的任何幫助。
更改此:
oneRepMax? = Measurement(value: result, unit: unit)
至:
oneRepMax = Measurement(value: result, unit: unit)
如果oneRepMax
是當前帶有nil
值的可選值,那么當您使用oneRepMax? = ...
時,賦值不會發生oneRepMax? = ...
oneRepMax? = ...
iOS / Swift的答案:無法將可選的String分配給UILabel文本屬性是相關的,並提供了更多解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.