# 如何从不同的角度引用常量的值 Swift

[英]How to reference a value of a constant from a different view Swift

``````import SwiftUI

struct OneRepMaxCalculator: View {

@State private var reps = 1.0
@State private var weight = 0.0

var body: some View {
Form{
TextField("Weight: \(weight.formatted())kg", value: \$weight, format: .number)
}
Stepper("Reps : \(reps.formatted())", value: \$reps, in: 1...10)
}

//THIS CONSTANT
let oneRepMax = reps == 1.0 ? weight : weight*(1+(0.0333*reps))-2

if oneRepMax > 0 {
}

if weight > 0 {
Text(String(format: "Your 2RM is %.1fkg", oneRepMax/(1+(0.0333*2))+2, oneRepMax/(1+(0.0333*2))+2))
.font(.footnote)
Text(String(format: "Your 3RM is %.1fkg", oneRepMax/(1+(0.0333*3))+2, oneRepMax/(1+(0.0333*3))+2))
.font(.footnote)
Text(String(format: "Your 4RM is %.1fkg", oneRepMax/(1+(0.0333*4))+2, oneRepMax/(1+(0.0333*4))+2))
.font(.footnote)
Text(String(format: "Your 5RM is %.1fkg", oneRepMax/(1+(0.0333*5))+2, oneRepMax/(1+(0.0333*5))+2))
.font(.footnote)
Text(String(format: "Your 6RM is %.1fkg", oneRepMax/(1+(0.0333*6))+2, oneRepMax/(1+(0.0333*6))+2))
.font(.footnote)
Text(String(format: "Your 7RM is %.1fkg", oneRepMax/(1+(0.0333*7))+2, oneRepMax/(1+(0.0333*7))+2))
.font(.footnote)
Text(String(format: "Your 8RM is %.1fkg", oneRepMax/(1+(0.0333*8))+2, oneRepMax/(1+(0.0333*8))+2))
.font(.footnote)
Text(String(format: "Your 9RM is %.1fkg", oneRepMax/(1+(0.0333*9))+2, oneRepMax/(1+(0.0333*9))+2))
.font(.footnote)
Text(String(format: "Your 10RM is %.1fkg", oneRepMax/(1+(0.0333*10))+2, oneRepMax/(1+(0.0333*10))+2))
.font(.footnote)
}
}
}

if oneRepMax > 0 {
}
}
}

if weight == 0 && reps > 1 {
.font(.callout)
}.foregroundColor(Color.red)

}

}
}
}

struct OneRepMaxCalculator_Previews: PreviewProvider {
static var previews: some View {
OneRepMaxCalculator()
.preferredColorScheme(.dark)
}
}
``````

``````import SwiftUI

var body: some View {
Form{
}
}
}
}

static var previews: some View {
}
}
``````

``````struct ProgressiveOverload: View {
let oneRepMax: Double
var body: some View {
Form{
}
}
}
}
``````

``````NavigationLink(destination: ProgressiveOverload(oneRepMax: oneRepMax)) {