I’m coding a Swift application for macOS.
I’m a beginner in Swift so my problem might seem stupid to you, I apologize for that…
I have an Identifiable containing the following two String variables:
struct ShortcutInfo: Identifiable {
let id: String
let name: String
}
The variable I use the most is “name”, containing a list of texts.
I tried to filter the list with a TextField. The filtered list should appear in the Picker.
@State var filteredShortcuts = ShortcutInfo(id: "", name: "").name.filter($0.lowercased().contains(searchText.lowercased()))
TextField("", text: $searchText)
Picker("", selection: $selection) {
ForEach(filteredShortcuts) { item in
Text(item)
}
}
But I get the error “Cannot use instance member ‘searchText’ within property initializer; property initializers run before ‘self’ is available”.
What is wrong in my code?
In addition, I am not sure of the validity of my code regarding the display of the list in the Picker.
Thanks in advance.