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
1 Answer
Reset to default 0try 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
版权声明:本文标题:swift - Conditions that depend on a numeric input from user and generates another numeric - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741604187a2387874.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论