admin管理员组

文章数量:1294652

I'm trying to create a formula where the user inputs the variables.

One of the components is Age. Above 70 years old, the factor in the formula is 110, otherwise it is 125

But i keep can't seem to get it working. I get "complier is unable to type-check this expression in a reasonable time". I've checked my brackets and everything else, and I'm not sure if i'm using the if else statement correctly, or whether if there is another function to use specifically for numerics

These are my State variables:

@State var Weight: String = ""
@State var Height: String = ""
@State var SaO2: String = ""
@State var SvO2: String = ""
@State var Hb: String = ""
@State var HR: String = ""
@State var Age: String = ""
@State var BSA: String = ""
@State var VO2: Double = 0   // I previously used String, but it doesn't work

And this is the code that is embedded within the Button action

if let Age = Double(Age),
   let Weight = Double(Weight),
   let Height = Double(Height),
   let SaO2 = Double(SaO2),
   let SvO2 = Double(SvO2),
   let Hb = Double(Hb),
   let HR = Double(HR),
   let BSA = Double(BSA) {

if Age < 70 {
    VO2 = 125
} else {
    VO2 = 110
}

let BSA = pow((Height * Weight, 0.5))
let CO = ((VO2) * BSA * Hb * 13.4) / (SaO2 - SvO2)
let CI = CO / BSA
let SV = CO / HR

answerCO = String(format: "%.2f", CO)
answerCI = String(format: "%.2f", CI)
answerSV = String(format: "%.2f", SV)

@State var VO2: Double = 0

I'm trying to create a formula where the user inputs the variables.

One of the components is Age. Above 70 years old, the factor in the formula is 110, otherwise it is 125

But i keep can't seem to get it working. I get "complier is unable to type-check this expression in a reasonable time". I've checked my brackets and everything else, and I'm not sure if i'm using the if else statement correctly, or whether if there is another function to use specifically for numerics

These are my State variables:

@State var Weight: String = ""
@State var Height: String = ""
@State var SaO2: String = ""
@State var SvO2: String = ""
@State var Hb: String = ""
@State var HR: String = ""
@State var Age: String = ""
@State var BSA: String = ""
@State var VO2: Double = 0   // I previously used String, but it doesn't work

And this is the code that is embedded within the Button action

if let Age = Double(Age),
   let Weight = Double(Weight),
   let Height = Double(Height),
   let SaO2 = Double(SaO2),
   let SvO2 = Double(SvO2),
   let Hb = Double(Hb),
   let HR = Double(HR),
   let BSA = Double(BSA) {

if Age < 70 {
    VO2 = 125
} else {
    VO2 = 110
}

let BSA = pow((Height * Weight, 0.5))
let CO = ((VO2) * BSA * Hb * 13.4) / (SaO2 - SvO2)
let CI = CO / BSA
let SV = CO / HR

answerCO = String(format: "%.2f", CO)
answerCI = String(format: "%.2f", CI)
answerSV = String(format: "%.2f", SV)

@State var VO2: Double = 0
Share Improve this question edited Feb 15 at 4:08 John Kugelman 362k69 gold badges552 silver badges596 bronze badges asked Feb 12 at 11:26 Alfred WongAlfred Wong 252 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

try using this:

 let BSA = pow(Height * Weight, 0.5)

EDIT-1:

Here some example code to show how to use Double all the way.

Note, you should conform to the common practice of naming your variables starting in lowercase.

struct ContentView: View {
    @State private var weight: Double = 1
    @State private var height: Double = 2
    @State private var saO2: Double = 3
    @State private var svO2: Double = 4
    @State private var hb: Double = 5
    @State private var hR: Double = 6
    @State private var age: Double = 7
    @State private var bSA: Double = 8
    @State private var vO2: Double = 0
    
    @State private var answerCO = "0"
    @State private var answerCI = "0"
    @State private var answerSV = "0"
    
    var body: some View {
        VStack (spacing: 33) {
            // input
            List {
                TextField("Weight", value: $weight, format: .number)
                TextField("Height", value: $height, format: .number)
                TextField("SaO2", value: $saO2, format: .number)
                TextField("SvO2", value: $svO2, format: .number)
                TextField("Hb", value: $hb, format: .number)
                TextField("HR", value: $hR, format: .number)
                TextField("Age", value: $age, format: .number)
            }

            // results
            Text("Answers")
            HStack {
                Text(answerCO)
                Text(answerCI)
                Text(answerSV)
            }.foregroundStyle(.red)
            
            // calculations
            Button("Calculate") {
                if age < 70 {
                    vO2 = 125
                } else {
                    vO2 = 110
                }
                
                bSA = pow(height * weight, 0.5)
                let cO = ((vO2) * bSA * hb * 13.4) / (saO2 - svO2)
                let cI = cO / bSA
                let sV = cO / hR
                
                answerCO = String(format: "%.2f", cO)
                answerCI = String(format: "%.2f", cI)
                answerSV = String(format: "%.2f", sV)
            }
        }.buttonStyle(.borderedProminent)
    }
}

本文标签: swiftConditions that depend on a numeric input from user and generates another numericStack Overflow