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..
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()
}
}
I would suggest to extract a function or computed property for that.
You can make it a free function in the same file. Then you will need to pass weight and reps as parameters, but on a plus point you can also place the inverse function nearby. There is some aesthetics in such symmetry. And free functions are easy to cover with unit tests.
func oneRepMaxFromWeight(_ weight: Double, reps: Double) -> Double {
if reps == 1 {
return weight
} else {
return weight*(1+(0.0333*reps))-2
}
}
func weightFromOneRepMax(_ oneRepMax, reps: Double) -> Double {
if reps == 1 {
return oneRepMax
} else {
// I think that's not a correct inversion of the previous formula.
// Looks like it should be (oneRepMax + 2)/(1 + 0.0333 * reps)
return oneRepMax/(1+(0.0333*reps))+2
}
}
Alternatively you can make oneRepMax a computed property and maybe a function for weightForReps(). But then oneRepMax is computed every time weightForReps() is computed. And it's harder to write unit tests.
struct ContentView: View {
@State private var weight = 1.0
@State private var reps = 1.0
var oneRepMax: Double {
if reps == 1 {
return weight
} else {
return weight*(1+(0.0333*reps))-2
}
}
func weightForReps(_ reps: Double) -> Double {
if reps == 1 {
return oneRepMax
} else {
// I think that's not a correct inversion of the previous formula.
// Looks like it should be (oneRepMax + 2)/(1 + 0.0333 * reps)
return oneRepMax/(1+(0.0333*reps))+2
}
}
...
}
I tried it on a couple of my lifting records and it seems like pseudoscience. How can reps be meaningful? Unless you do a 1RM, there's giant variability in what you're lifting based on how much time is allotted for the set, and the curve of where you log the reps in that set.