Making a Generic View

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:
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!
Accepted Answer
As you found, as-casting from Array<T> to Array<SomeProtocol> is an always-fail conversion in Swift, even if T conforms to SomeProtocol.

Maybe the easiest way is making your View generic:
Code Block
struct SearchablePicker<SearchableType: SearchableObject>: View {
@Binding var searchText: String
@Binding var selectedValue: Int
@Binding var searchableList: [SearchableType]
var body: some View {
//...
}
}


But I cannot be sure if this would work or not, nor this would be the best solution for you or not.
The behavior of SwiftUI may be affected by many other parts of your code. You should better show enough code to confirm.
@OOPer Thanks for your response. I think that was exactly the correct solution. I didn't even think of that possibility. Everything seems to be working as expected now.
Making a Generic View
 
 
Q