The following picker isn't updating $selection. Regardless of what the Picker shows while running the app, selection.rawValue always returns 0.
What is preventing the Picker from updating the State variable selection?
import SwiftUI
struct OrderPicker: View {
let initialIndex: Int
@State private var selection: Index
enum Index: Int, CaseIterable, Identifiable {
case none = 0,
first = 1,
second = 2,
third = 3
var id: Self { self }
}
init(initialIndex: Int) {
self.initialIndex = initialIndex
_selection = State(initialValue: OrderPicker.Index(rawValue: initialIndex) ?? .none)
}
var body: some View {
Form {
Picker("Order in list", selection: $selection) {
ForEach(Index.allCases) { index in
Text(String(describing: index)).tag(index)
}
}
}
.frame(height: 116)
}
func getOrderIndex() -> Int {
let index = selection.rawValue
return index
}
}
selection. You can verify this by adding.onChange(of: selection) { print($0) }to the end of yourForm. However, the presence ofgetOrderIndex, which is unused in your current code, makes me wonder if you're trying to do something like reach into a child view to see it's state from the parent view or something like that.selectionin a parent class by exposing it withgetOrderIndex. WillgetOrderIndexnot accurately returnselection's rawValue?