使用公式输出作为变量

Using formula output as variable

我正在尝试采用以下 HStack:

HStack {
  Text("BUN/Creatinine Ratio")
  Spacer()
  Text("\((coredata.one / coredata.two), specifier: "%.0f")")
}

并应用以下 if...then 语句:

if let numberValue = Double(formulaOutput) { //Output from the above formula.
  if (10...20) ~= numberValue {
    Image(systemName: "checkmark.circle.fill")
        .foregroundColor(Color(UIColor.systemGreen))
  }
  else {
    Image(systemName: "exclamationmark.circle.fill")
        .foregroundColor(Color(UIColor.systemRed))
  }
}

我只是不知道如何将输出用作变量。另外,是否可以为“else”语句定义多个范围? <10 = 值 1,>20 = 值 2?

谢谢!

试试这个示例代码。我还建议您再次阅读 Swift 和 SwiftUI 基础知识:

let formulaOutput: Double = (coredata.one / coredata.two)
HStack {
    Text("BUN/Creatinine Ratio")
    Spacer()
    Text("\(formulaOutput, specifier: "%.0f")")
}

if (10...20) ~= formulaOutput {
    Image(systemName: "checkmark.circle.fill")
        .foregroundColor(Color(UIColor.systemGreen))
} else {
    Image(systemName: "exclamationmark.circle.fill")
        .foregroundColor(Color(UIColor.systemRed))
}