Reputation: 11
I'm attempting to figure out how I can use the value of the index in my picker for an equation, rather than usng the selected number of the picker. I am not having success
Following is my array:
var wheelHeights = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70]
User, via picker, will select their preferred wheel height above the ground.
My code then goes through a few (a lot actually) if statement sections, and finally to my issue...
var wheelHeightFactor: Double {
let wheelFactor = Double(wheelHeight)
let densityWF = Double(densityWeightFactor)
if wheelFactor <= 21 && densityWeightFactor >= 2.9 {
let whf = Double((wheelFactor) * 0.2) + densityWeightFactor
return whf
I'm looking to make wheelHeight in this equation be the picker index value, rather than the actual wheelHeight.
Following is what the code does now:
let whf = Double((15) * 0.2) + densityWeightFactor
return whf
The equation SHOULD look like this in terms of where my numbers are if user selected 15:
let whf = Double((3) * 0.2) + densityWeightFactor
return whf
Thank you all in advance
Upvotes: 1
Views: 607
Reputation: 2355
You will have to create a seperate @State
variable to keep track of the actual index:
Consider the following example:
struct ContentView: View {
let data = [1,5,10,20]
@State private var selectedIndex = 0
var body: some View {
VStack {
Text("Current index: \(selectedIndex)")
Picker("Your Selection", selection: $selectedIndex) {
ForEach(0..<data.count) { index in
Text("\(data[index])")
}
}
}
}
}
So if I understand your example right, then you want to use the actual index instead of wheelFactor
. If so, you could do something like this:
@State private var selectedWheelHeightIndex = 0
//...
let whf = Double((selectedWheelHeightIndex) * 0.2) + densityWeightFactor
return whf
(I assume that you are using SwiftUI)...
Upvotes: 3