You can achieve this by adding another @State variable to store the interpretation text and updating it inside your button's action. Here's how you can modify your code:
@State var interpretation: String = "" // New state variable for interpretation
Button {
// Ensure all inputs are valid numbers
if let setVT = Double(SetVT), let pplat = Double(Pplat),
let lPEEP = Double(LPEEP), let hPEEP = Double(HPEEP),
let vtTotal = Double(VTtotal), let vtHPEEP = Double(VTHPEEP) {
let Cbaby = setVT / (pplat - lPEEP)
let pVrec = hPEEP - lPEEP
let ppVrec = pVrec * Cbaby
let Vrec = vtTotal - vtHPEEP - ppVrec
let Crec = Vrec / (hPEEP - lPEEP)
let raw = max(Crec / Cbaby, 0)
answer = String(format: "%.2f", raw)
// Interpretation logic
interpretation = raw < 0.5 ? "XXX" : "YYY"
} else {
answer = "Invalid input"
interpretation = ""
}
} label: {
Text("Calculate")
.fontWeight(.bold)
.frame(width: 200.0, height: 50)
.background(Color.blue)
.cornerRadius(10)
.foregroundColor(.white)
.font(.system(size: 20))
.padding()
}
// Display result and interpretation
Text("RI = \(answer)")
.font(.system(size: 20))
.fontWeight(.bold)
Text(interpretation)
.font(.system(size: 18))
.foregroundColor(.gray)
Explanation:
@State var interpretation: String = ""
to store the interpretation.(raw < 0.5 ? "XXX" : "YYY")
to assign the appropriate interpretation.Now, when the user clicks the button, the calculated result will be displayed along with a meaningful interpretation!