I am complete beginner so I am sorry if my question will sound dumb.. But where should I add the if statement in the actual app? All the tutorials I could find are either from the Xcode playground or aren't helpful at all.. Thanks in advance
I am trying to do something like this: (Basically I want the oneRepMax to be equal to weight if the number of reps is 1)
if reps == 1 {
oneRepMax == weight
} else {
oneRepMax = weight*(1 (0.0333*reps))-2
}
Here is my code:
//
// ContentView.swift
// overload
//
// Created by / on 25.08.2022.
//
import SwiftUI
struct ContentView: View {
@State private var weight = 1.0
@State private var reps = 1.0
var body: some View {
VStack{
Stepper("Reps : \(reps.formatted())", value: $reps, in: 1...10)
TextField("Weight: \(weight.formatted())kg", value: $weight, format: .number)
.keyboardType(.numberPad)
let oneRepMax=weight*(1 (0.0333*reps))-2
Text(String(format: "Your 1RM is %.1f kg", oneRepMax, oneRepMax)).padding()
Group {
Text(String(format: "Your 2RM is %.1f kg", oneRepMax/(1 (0.0333*2)) 2, oneRepMax/(1 (0.0333*2)) 2))
Text(String(format: "Your 3RM is %.1f kg", oneRepMax/(1 (0.0333*3)) 2, oneRepMax/(1 (0.0333*3)) 2))
Text(String(format: "Your 4RM is %.1f kg", oneRepMax/(1 (0.0333*4)) 2, oneRepMax/(1 (0.0333*4)) 2))
Text(String(format: "Your 5RM is %.1f kg", oneRepMax/(1 (0.0333*5)) 2, oneRepMax/(1 (0.0333*5)) 2))
Text(String(format: "Your 6RM is %.1f kg", oneRepMax/(1 (0.0333*6)) 2, oneRepMax/(1 (0.0333*6)) 2))
Text(String(format: "Your 7RM is %.1f kg", oneRepMax/(1 (0.0333*7)) 2, oneRepMax/(1 (0.0333*7)) 2))
Text(String(format: "Your 8RM is %.1f kg", oneRepMax/(1 (0.0333*8)) 2, oneRepMax/(1 (0.0333*8)) 2))
Text(String(format: "Your 9RM is %.1f kg", oneRepMax/(1 (0.0333*9)) 2, oneRepMax/(1 (0.0333*9)) 2))
Text(String(format: "Your 10RM is %.1f kg", oneRepMax/(1 (0.0333*10)) 2, oneRepMax/(1 (0.0333*10)) 2))
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
CodePudding user response:
try this example code:
struct ContentView: View {
@State private var weight = 1.0
@State private var reps = 1.0
var body: some View {
VStack{
Stepper("Reps : \(reps.formatted())", value: $reps, in: 1...10)
TextField("Weight: \(weight.formatted())kg", value: $weight, format: .number)
.keyboardType(.numberPad)
let oneRepMax = Int(reps) == 1 ? weight : weight*(1 (0.0333*reps))-2 // <-- here
// -- here, no need for the second oneRepMax...
Text(String(format: "Your 1RM is %.1f kg", oneRepMax)).padding()
Group {
// -- here, no need for the second oneRepMax...
Text(String(format: "Your 2RM is %.1f kg", oneRepMax/(1 (0.0333*2)) 2))
Text(String(format: "Your 3RM is %.1f kg", oneRepMax/(1 (0.0333*3)) 2))
Text(String(format: "Your 4RM is %.1f kg", oneRepMax/(1 (0.0333*4)) 2))
Text(String(format: "Your 5RM is %.1f kg", oneRepMax/(1 (0.0333*5)) 2))
Text(String(format: "Your 6RM is %.1f kg", oneRepMax/(1 (0.0333*6)) 2))
Text(String(format: "Your 7RM is %.1f kg", oneRepMax/(1 (0.0333*7)) 2))
Text(String(format: "Your 8RM is %.1f kg", oneRepMax/(1 (0.0333*8)) 2))
Text(String(format: "Your 9RM is %.1f kg", oneRepMax/(1 (0.0333*9)) 2))
Text(String(format: "Your 10RM is %.1f kg", oneRepMax/(1 (0.0333*10)) 2))
}
}
}
}
CodePudding user response:
All you need to do is create a computed property that returns the result you need, like so:
struct ContentView: View {
@State private var weight = 1.0
@State private var reps = 1.0
private var oneRepMax: Double {
if reps == 1 {
return weight
} else {
return weight*(1 (0.0333*reps))-2
}
}
var body: some View {
...
}
}
Here, every time ContentView
is redrawn, that is every time one of the @State
properties changes, the oneRepMax
property will be recalculated, staying up-to-date.