Paul,
First, thanks for your site! It's great!
I to have a picker that I want to load from SwiftData. My issue is I need to initialize 'Picker( selection' to avoid the error:
Picker: the selection "" is invalid and does not have an associated tag, this will give undefined results.
I want to set 'selection' to the first element in the table.
I tried this code:
@State var selectedCategory:String = ""
@Query(sort:\ToolCategoriesEnt.catName) var categories: [ToolCategoriesEnt]
init(
selectedCategory: String = State(initialValue: categories.first?.catName),
This gives two errors:
Cannot use instance member 'categories' as a default parameter
Default argument value of type 'State<String?>' cannot be converted to type 'String'
I also tried:
Picker(selection: $selectedCategory,
content: {
ForEach(categories) { categorie in
Text(categorie.catName)
.tag(categorie.catName)
}
}, label: {} ).onAppear(perform: { selectedCategory = categories.first?.catName ?? "nil" })
This gives me an empty string not the first element.
Any suggestions would help!
Thanks for all you do!