Downcasting of environment object in SwiftUI

Hi there,

I’m storing a heterogenous array of devices [Device] in an environment object. Now i want to iterate over it and have a different subview for each different struct (that implements the Device protocol) in the array.

I was able to get the above functionality by doing a type check with is and conditionally using different sub-views.

Now in my subview i am unable to bind a value from a downcasted device(for example in a slider).
Slider(value: (device as SpecialDevice).attribute)

There must be a better way to handle heterogenous arrays and downcasting in combination with two-way data binding, right?

@rrrrobin Do you still have issues with this?

Yes, do you have any suggestions?

I have refactored my device protocol to have a function that returns the devices view thought which makes the code cleaner. But i still have not figured out the best way to update the UI when I change my data in my @Published devices

Hi @rrrrobin,
did you get a chance to look at @Binding , this is used to have a state for the UI but the data comes from an external source.

cheers,

This topic was automatically closed after 166 days. New replies are no longer allowed.