I'll admit first that I'm still somewhat new to swift and swiftui and developing in native code for the Apple platforms (the app I work on at work is built for iOS with Cordova), but my current project (for me, not work) is all built in swift and I'm using swiftui for the interface.
The issue that I ran into today was this: I'm trying to build a generic view type that can take some bindings and produce a searchable picker. I've done that, but my problem comes with the array to search. The elements of the array that I want to pass in are of a type that conforms to a simple protocol I built and so I defined the type of the array in my new picker view to be an array of the protocol, but then when I try to pass the array of actual objects to the view, I get an error that the array is not of the correct type. I know I can downcast to the protocol's type, but I don't know how to do that with a binding involved. Everything I've tried just keeps showing an error that it can't convert the type
Where I've defined it:
Protocol:
Where I'm using it:
Project conforms to the SearchableObject protocol so I thought I could try:
But, Xcode gives me a warning that that conversion always fails. So, how do I get my array of Projects passed into my SearchablePicker which needs to get an array of objects which have ids and names?
Thank you all for reading this far and I appreciate any help you can offer!
The issue that I ran into today was this: I'm trying to build a generic view type that can take some bindings and produce a searchable picker. I've done that, but my problem comes with the array to search. The elements of the array that I want to pass in are of a type that conforms to a simple protocol I built and so I defined the type of the array in my new picker view to be an array of the protocol, but then when I try to pass the array of actual objects to the view, I get an error that the array is not of the correct type. I know I can downcast to the protocol's type, but I don't know how to do that with a binding involved. Everything I've tried just keeps showing an error that it can't convert the type
Where I've defined it:
Code Block swift struct SearchablePicker: View { @Binding var searchText: String @Binding var selectedValue: Int @Binding var searchableList: [SearchableObject]
Protocol:
Code Block swift protocol SearchableObject { var id: Int? {get set } var name: String? {get set } }
Where I'm using it:
Code Block swift @State var projects = [Project]() @State var searchText: String = "" @State var selectedProject: Int = 0 var body: some View { NavigationView { Form { Section { SearchablePicker(searchText: $searchText, selectedValue: $selectedProject, searchableList: $projects) }
Project conforms to the SearchableObject protocol so I thought I could try:
Code Block swift SearchablePicker(searchText: $searchText, selectedValue: $selectedProject, searchableList: $projects as! Binding<[SearchableObject]> )
But, Xcode gives me a warning that that conversion always fails. So, how do I get my array of Projects passed into my SearchablePicker which needs to get an array of objects which have ids and names?
Thank you all for reading this far and I appreciate any help you can offer!