0

I'm developing a screen in SwiftUI and have the following code:

...

@EnvironmentObject var externalState: MainStateObject

...

SelectOptionPopover(options: $externalState.depots,
                            selectedOption: selectedDepot,
                            prompt: "Depot: ")

...

SelectOptionPopover is a view that I created to handle a variety of popovers. For the options, it expects an array of [SelectOptionPopoverOption], which is declared like this:

protocol SelectOptionPopoverOption {
    var displayName: String { get }
}

Now, the issue I have is that when I pass an array of SelectOptionPopoverOptions, it works just fine. But if I pass an array of another type that conforms to SelectOptionPopoverOptions, the conversion fails with something like:

'Binding<[Depot]>' is not convertible to 'Binding<[SelectOptionPopoverOption]>'

These may be the exact same objects, but work when they're identified as SelectOptionPopoverOptions but not when identified as a Depots.

I can work around this by using arrays of SelectedOptionPopoverOption and casting them as needed, but it would sure be cleaner to be able to use the conforming types instead.

Any ideas on how I could use the more specific types instead?

1 Answer 1

1

You can declare and adopt your custom SelectOptionPopover view as generics

struct SelectOptionPopover<T>: View where T: SelectOptionPopoverOption {
    @Binding var options: [T]
    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.