Home > database >  Using formula output as variable
Using formula output as variable

Time:05-01

I'm trying to take the following HStack:

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

And apply the following if...then statement to it:

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))
  }
}

I just can't figure out how to use the output as a variable. Also, is it possible to define more than one range for the "else" statement? <10 = Value 1, >20 = Value 2?

Thanks!

CodePudding user response:

try this sample code. I also suggest you read again the Swift and SwiftUI basics:

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))
}
  • Related