0

I am attempting to create a 'Shortcuts' style interface where you can pick one or more actions from a list and add them to your 'workflow'. This would involve dynamically inserting views into an array. I realise this can be done using AnyView but with the performance implications it has this seems like a bad idea.

My initial (perhaps naive) approach was to do the following:


struct WorkflowView<Action: View>: View {
  
  var actionItems: [Action] = []

  var body: some View {
    ...
  }
}

But this causes an issue at the call site since the compiler needs to know the type for the array you are passing in

  WorkflowView(actionItems: [Text("Hi"), Image(systemImage: "circle")]) // Compiler error as it can't determine the type for the array

Is there any way to do this without using AnyView?

3
  • 3
    It is not about SwiftUI it is swift static typing, you try to put into array instances of different types. Think instead about using instances of same model type and generate view dynamically, depending on model value. Here is an example of approach stackoverflow.com/a/63611331/12299030. Commented Apr 28, 2022 at 11:29
  • @Chris: All the Asperi said I say also, plus it is possible to do the thing you are trying to do but it needs deep understanding of SwiftUI protocols, one small help for you, you need ViewBuilder wrapper also you need TupleView and instead of <Action: View> you are going work with <Action1: View, Action2: View, Action3: View, ...> Commented Apr 28, 2022 at 13:14
  • You should watch the Demystifying SwiftUI video from WWDC21 Commented Apr 28, 2022 at 13:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.