I'm trying to use Picker
from SwiftUI, I had this code working for iOS 14 but when I updated the project to work with iOS 15 only now it's broken and Picker is not updating anymore after assigning @State
on view appear.
I have created a new project from zero to test it and yes I could replicate the error.
here is the code I'm using:
import SwiftUI
struct Category: Identifiable {
let id: UUID = UUID()
let name: String
}
let categories: [Category] = [
Category(name: "Cat 1"),
Category(name: "Cat 2"),
Category(name: "Cat 3")
]
struct ContentView: View {
@State private var selectedCategory = -1
var body: some View {
NavigationView {
Form {
Section {
Picker("Category", selection: $selectedCategory) {
ForEach(0..<categories.count) {
Text(categories[$0].name)
.tag($0)
}
}
}
}
.onAppear {
self.selectedCategory = 1
}
}
}
}
as you see in onAppear
block, I'm changing selectedCategory
value, this peace of code works well in iOS 14 but not iOS 15.
thanks