【问题标题】:Missing return in a function - swift error in Model函数中缺少返回 - 模型中的快速错误
【发布时间】:2015-04-22 17:25:47
【问题描述】:

所以我在情节提要上有自己生成的饼图,其中包含各种标签、滑块、年龄、CurrentAsset、收入等按钮。我正在使用 struct 构建模型,并且我不断收到几种“缺少返回”的方法的错误在预期返回“双”的函数中”,即使您可以看到我有 currentAssetFactor、savingFactor、bondFactor、toleranceFactor、outlookFactor 的返回值。我做错了什么?

public struct AllocationModel
{
  let maxAge = 95.0
  let currentAge: Double

  let minCurrentAsset = 100000.0
  let maxCurrentAsset = 500000.0
  let currentAsset: Double

  let minSavings = 5000.0
  let maxSavings = 20000.0
  let currentSavings: Double

  let maxIncomeRequired = 0.04
  let minIncomeRequired = 0.01
  let currentIncomeRequired: Double

  let tolerance: String

  let outlook: String

  // MARK: - Methods

  public var currentAssetFactor: Double {
    if currentAsset > maxCurrentAsset {
      return 5.0
    } else if currentAsset >= minCurrentAsset {
      return Double(currentAsset / minCurrentAsset)
    }
  }

  public var savingsFactor: Double {
    if currentSavings >= maxSavings {
      return 4.0
    } else if currentSavings >= minSavings {
      return Double(currentSavings / minSavings)
    }
  }

  public var bondFactor: Double {
    if currentIncomeRequired >= maxIncomeRequired {
      return 0.2
    } else if currentIncomeRequired >= minIncomeRequired {
      return (0.05 * Double(currentIncomeRequired / minIncomeRequired) )
    }
  }

  public var toleranceFactor: Double {
    switch tolerance {
      case "conservative": return 0.0
      case "balanced": return 3.0
      case "aggressive": return 6.0
    default: 0.0
    }
  }

  public var outlookFactor: Double {
    switch outlook {
      case "poor": return 0.0
      case "moderate": return 7.0
      case "strong": return 10.0
    default: return 0.0
    }
  }


  public var stock: Double {
    return maxAge - currentAge + currentAssetFactor + savingsFactor + toleranceFactor + outlookFactor
  }

  public var nonStock: Double { return 100.0 - stock }

  public var bondPercentage: Double { return 0.35 + bondFactor }

  public var bond: Double { return nonStock * bondPercentage }

  public var cash: Double { return nonStock - bond }

  // MARK: - Init

  public init(currentAge: Double, currentAsset: Double, currentSavings: Double, currentIncomeRequired: Double, tolerance: String, outlook: String) {
    assert(currentAge > 0)

    self.currentAge = currentAge
    self.currentAsset = currentAsset
    self.currentSavings = currentSavings
    self.currentIncomeRequired = currentIncomeRequired
    self.tolerance = tolerance
    self.outlook = outlook
  }

}

【问题讨论】:

    标签: ios swift struct model compiler-errors


    【解决方案1】:

    您并未涵盖所有情况。考虑您的一种方法:

      public var currentAssetFactor: Double {
        if currentAsset > maxCurrentAsset {
          return 5.0
        } else if currentAsset >= minCurrentAsset {
          return Double(currentAsset / minCurrentAsset)
        }
      }
    

    很好。那么如果第一个if 失败(例如currentAsset 大于maxCurrentAsset)而第二个if 失败(例如currentAsset 大于或等于minCurrentAsset)?现在我们什么都不退!

    所以你的逻辑有漏洞。而且编译器很聪明——比你还聪明! 如此聪明,它发现了你逻辑中的这个漏洞,并阻止你前进。您需要重写您的方法,以便向编译器证明涵盖了所有可能性,即无论发生什么情况,您都将始终返回一个值。

    【讨论】:

    • 你应该考虑把它写成案例陈述......它会帮助你抓住漏掉的案例
    • 添加到这个答案,在outlookFactor 默认:在你忘记return 0.0的开关内
    • @Jeef 你对那个“currentAssetFactor”例子的case语句是什么意思?我只能想到一个 Switch,但我不确定我能不能写出那个具体的例子。
    • @matt。谢谢。你说得对,我的逻辑有一个漏洞。我不应该假设 return 0.0 for else。
    猜你喜欢
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-04
    相关资源
    最近更新 更多