@Binding var in a spm package

26 Views Asked by At

I have the following View in a SPM pack that I intend to use in a few apps I am creating. The Idea is to have a generic Picker View that I can send list of objects and expect the user to pick one to return to its calling view. It does work well if I put the view inside my own app and not on the spm package, everything works fine. The error message says that the init on PickerListView needs to be public. If I do this, it complains that Cannot assign value of type 'Binding<T>' to type 'T'. If I

public protocol Listable {
    var name: String { get set }
}

public struct PickerListView<T: Listable & Hashable>: View {
    @Binding public var selected: T
    public var items: [T]
    
    public var body: some View {
        VStack {
            List(items, id: \.self) { item in
                Button(action: {
                    self.selected = item
                }) {
                    Text(item.name)
                }
            }
            .padding()
        }
    }
}

enter image description here

Also tried to remove the Binding. Did not work. Any ideas How I can solve this?

0

There are 0 best solutions below